LUCENE-7028: Remove duplicate method in LegacyNumericUtils

This commit is contained in:
Uwe Schindler 2016-02-14 00:49:21 +01:00
parent 42ae21cb9a
commit 74421d729b
8 changed files with 38 additions and 60 deletions

View File

@ -90,6 +90,9 @@ API Changes
* LUCENE-6947: SortField.missingValue is now protected. You can read its value
using the new SortField.getMissingValue getter. (Adrien Grand)
* LUCENE-7028: Remove duplicate method in LegacyNumericUtils.
(Uwe Schindler)
Optimizations
* LUCENE-6891: Use prefix coding when writing points in

View File

@ -91,7 +91,7 @@ public final class LegacyNumericUtils {
/**
* The maximum term length (used for <code>byte[]</code> buffer size)
* for encoding <code>long</code> values.
* @see #longToPrefixCodedBytes
* @see #longToPrefixCoded
*/
public static final int BUF_SIZE_LONG = 63/7 + 2;
@ -104,7 +104,7 @@ public final class LegacyNumericUtils {
/**
* The maximum term length (used for <code>byte[]</code> buffer size)
* for encoding <code>int</code> values.
* @see #intToPrefixCodedBytes
* @see #intToPrefixCoded
*/
public static final int BUF_SIZE_INT = 31/7 + 2;
@ -117,30 +117,6 @@ public final class LegacyNumericUtils {
* @param bytes will contain the encoded value
*/
public static void longToPrefixCoded(final long val, final int shift, final BytesRefBuilder bytes) {
longToPrefixCodedBytes(val, shift, bytes);
}
/**
* Returns prefix coded bits after reducing the precision by <code>shift</code> bits.
* This is method is used by {@link org.apache.lucene.analysis.LegacyNumericTokenStream}.
* After encoding, {@code bytes.offset} will always be 0.
* @param val the numeric value
* @param shift how many bits to strip from the right
* @param bytes will contain the encoded value
*/
public static void intToPrefixCoded(final int val, final int shift, final BytesRefBuilder bytes) {
intToPrefixCodedBytes(val, shift, bytes);
}
/**
* Returns prefix coded bits after reducing the precision by <code>shift</code> bits.
* This is method is used by {@link org.apache.lucene.analysis.LegacyNumericTokenStream}.
* After encoding, {@code bytes.offset} will always be 0.
* @param val the numeric value
* @param shift how many bits to strip from the right
* @param bytes will contain the encoded value
*/
public static void longToPrefixCodedBytes(final long val, final int shift, final BytesRefBuilder bytes) {
// ensure shift is 0..63
if ((shift & ~0x3f) != 0) {
throw new IllegalArgumentException("Illegal shift value, must be 0..63; got shift=" + shift);
@ -159,16 +135,15 @@ public final class LegacyNumericUtils {
}
}
/**
* Returns prefix coded bits after reducing the precision by <code>shift</code> bits.
* This is method is used by {@link org.apache.lucene.analysis.LegacyNumericTokenStream}.
* After encoding, {@code bytes.offset} will always be 0.
* After encoding, {@code bytes.offset} will always be 0.
* @param val the numeric value
* @param shift how many bits to strip from the right
* @param bytes will contain the encoded value
*/
public static void intToPrefixCodedBytes(final int val, final int shift, final BytesRefBuilder bytes) {
public static void intToPrefixCoded(final int val, final int shift, final BytesRefBuilder bytes) {
// ensure shift is 0..31
if ((shift & ~0x1f) != 0) {
throw new IllegalArgumentException("Illegal shift value, must be 0..31; got shift=" + shift);
@ -218,7 +193,7 @@ public final class LegacyNumericUtils {
* This method can be used to decode a term's value.
* @throws NumberFormatException if the supplied {@link BytesRef} is
* not correctly prefix encoded.
* @see #longToPrefixCodedBytes
* @see #longToPrefixCoded
*/
public static long prefixCodedToLong(final BytesRef val) {
long sortableBits = 0L;
@ -242,7 +217,7 @@ public final class LegacyNumericUtils {
* This method can be used to decode a term's value.
* @throws NumberFormatException if the supplied {@link BytesRef} is
* not correctly prefix encoded.
* @see #intToPrefixCodedBytes
* @see #intToPrefixCoded
*/
public static int prefixCodedToInt(final BytesRef val) {
int sortableBits = 0;
@ -427,8 +402,8 @@ public final class LegacyNumericUtils {
*/
public void addRange(final long min, final long max, final int shift) {
final BytesRefBuilder minBytes = new BytesRefBuilder(), maxBytes = new BytesRefBuilder();
longToPrefixCodedBytes(min, shift, minBytes);
longToPrefixCodedBytes(max, shift, maxBytes);
longToPrefixCoded(min, shift, minBytes);
longToPrefixCoded(max, shift, maxBytes);
addRange(minBytes.get(), maxBytes.get());
}
@ -456,8 +431,8 @@ public final class LegacyNumericUtils {
*/
public void addRange(final int min, final int max, final int shift) {
final BytesRefBuilder minBytes = new BytesRefBuilder(), maxBytes = new BytesRefBuilder();
intToPrefixCodedBytes(min, shift, minBytes);
intToPrefixCodedBytes(max, shift, maxBytes);
intToPrefixCoded(min, shift, minBytes);
intToPrefixCoded(max, shift, maxBytes);
addRange(minBytes.get(), maxBytes.get());
}

View File

@ -361,9 +361,9 @@ public class TestNumericRangeQuery32 extends LuceneTestCase {
}
final BytesRef lowerBytes, upperBytes;
BytesRefBuilder b = new BytesRefBuilder();
LegacyNumericUtils.intToPrefixCodedBytes(lower, 0, b);
LegacyNumericUtils.intToPrefixCoded(lower, 0, b);
lowerBytes = b.toBytesRef();
LegacyNumericUtils.intToPrefixCodedBytes(upper, 0, b);
LegacyNumericUtils.intToPrefixCoded(upper, 0, b);
upperBytes = b.toBytesRef();
// test inclusive range

View File

@ -385,9 +385,9 @@ public class TestNumericRangeQuery64 extends LuceneTestCase {
}
final BytesRef lowerBytes, upperBytes;
BytesRefBuilder b = new BytesRefBuilder();
LegacyNumericUtils.longToPrefixCodedBytes(lower, 0, b);
LegacyNumericUtils.longToPrefixCoded(lower, 0, b);
lowerBytes = b.toBytesRef();
LegacyNumericUtils.longToPrefixCodedBytes(upper, 0, b);
LegacyNumericUtils.longToPrefixCoded(upper, 0, b);
upperBytes = b.toBytesRef();
// test inclusive range

View File

@ -29,7 +29,7 @@ public class TestLegacyNumericUtils extends LuceneTestCase {
BytesRefBuilder last = new BytesRefBuilder();
BytesRefBuilder act = new BytesRefBuilder();
for (long l=-100000L; l<100000L; l++) {
LegacyNumericUtils.longToPrefixCodedBytes(l, 0, act);
LegacyNumericUtils.longToPrefixCoded(l, 0, act);
if (last!=null) {
// test if smaller
assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0 );
@ -47,7 +47,7 @@ public class TestLegacyNumericUtils extends LuceneTestCase {
BytesRefBuilder act = new BytesRefBuilder();
BytesRefBuilder last = new BytesRefBuilder();
for (int i=-100000; i<100000; i++) {
LegacyNumericUtils.intToPrefixCodedBytes(i, 0, act);
LegacyNumericUtils.intToPrefixCoded(i, 0, act);
if (last!=null) {
// test if smaller
assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0 );
@ -69,7 +69,7 @@ public class TestLegacyNumericUtils extends LuceneTestCase {
for (int i=0; i<vals.length; i++) {
prefixVals[i] = new BytesRefBuilder();
LegacyNumericUtils.longToPrefixCodedBytes(vals[i], 0, prefixVals[i]);
LegacyNumericUtils.longToPrefixCoded(vals[i], 0, prefixVals[i]);
// check forward and back conversion
assertEquals( "forward and back conversion should generate same long", vals[i], LegacyNumericUtils.prefixCodedToLong(prefixVals[i].get()) );
@ -92,7 +92,7 @@ public class TestLegacyNumericUtils extends LuceneTestCase {
final BytesRefBuilder ref = new BytesRefBuilder();
for (int i=0; i<vals.length; i++) {
for (int j=0; j<64; j++) {
LegacyNumericUtils.longToPrefixCodedBytes(vals[i], j, ref);
LegacyNumericUtils.longToPrefixCoded(vals[i], j, ref);
long prefixVal= LegacyNumericUtils.prefixCodedToLong(ref.get());
long mask=(1L << j) - 1L;
assertEquals( "difference between prefix val and original value for "+vals[i]+" with shift="+j, vals[i] & mask, vals[i]-prefixVal );
@ -109,7 +109,7 @@ public class TestLegacyNumericUtils extends LuceneTestCase {
for (int i=0; i<vals.length; i++) {
prefixVals[i] = new BytesRefBuilder();
LegacyNumericUtils.intToPrefixCodedBytes(vals[i], 0, prefixVals[i]);
LegacyNumericUtils.intToPrefixCoded(vals[i], 0, prefixVals[i]);
// check forward and back conversion
assertEquals( "forward and back conversion should generate same int", vals[i], LegacyNumericUtils.prefixCodedToInt(prefixVals[i].get()) );
@ -132,7 +132,7 @@ public class TestLegacyNumericUtils extends LuceneTestCase {
final BytesRefBuilder ref = new BytesRefBuilder();
for (int i=0; i<vals.length; i++) {
for (int j=0; j<32; j++) {
LegacyNumericUtils.intToPrefixCodedBytes(vals[i], j, ref);
LegacyNumericUtils.intToPrefixCoded(vals[i], j, ref);
int prefixVal= LegacyNumericUtils.prefixCodedToInt(ref.get());
int mask=(1 << j) - 1;
assertEquals( "difference between prefix val and original value for "+vals[i]+" with shift="+j, vals[i] & mask, vals[i]-prefixVal );

View File

@ -616,7 +616,7 @@ public class TestBlockJoin extends LuceneTestCase {
if (VERBOSE) {
System.out.println("DELETE parentID=" + deleteID);
}
LegacyNumericUtils.intToPrefixCodedBytes(deleteID, 0, term);
LegacyNumericUtils.intToPrefixCoded(deleteID, 0, term);
w.deleteDocuments(new Term("blockID", term.toBytesRef()));
joinW.deleteDocuments(new Term("blockID", term.toBytesRef()));
}

View File

@ -581,7 +581,7 @@ public class BBoxStrategy extends SpatialStrategy {
private Query makeNumberTermQuery(String field, double number) {
BytesRefBuilder bytes = new BytesRefBuilder();
LegacyNumericUtils.longToPrefixCodedBytes(LegacyNumericUtils.doubleToSortableLong(number), 0, bytes);
LegacyNumericUtils.longToPrefixCoded(LegacyNumericUtils.doubleToSortableLong(number), 0, bytes);
return new TermQuery(new Term(field, bytes.get()));
}

View File

@ -463,19 +463,19 @@ public class TrieField extends PrimitiveFieldType {
try {
switch (type) {
case INTEGER:
LegacyNumericUtils.intToPrefixCodedBytes(Integer.parseInt(s), 0, result);
LegacyNumericUtils.intToPrefixCoded(Integer.parseInt(s), 0, result);
break;
case FLOAT:
LegacyNumericUtils.intToPrefixCodedBytes(LegacyNumericUtils.floatToSortableInt(Float.parseFloat(s)), 0, result);
LegacyNumericUtils.intToPrefixCoded(LegacyNumericUtils.floatToSortableInt(Float.parseFloat(s)), 0, result);
break;
case LONG:
LegacyNumericUtils.longToPrefixCodedBytes(Long.parseLong(s), 0, result);
LegacyNumericUtils.longToPrefixCoded(Long.parseLong(s), 0, result);
break;
case DOUBLE:
LegacyNumericUtils.longToPrefixCodedBytes(LegacyNumericUtils.doubleToSortableLong(Double.parseDouble(s)), 0, result);
LegacyNumericUtils.longToPrefixCoded(LegacyNumericUtils.doubleToSortableLong(Double.parseDouble(s)), 0, result);
break;
case DATE:
LegacyNumericUtils.longToPrefixCodedBytes(DateFormatUtil.parseMath(null, s).getTime(), 0, result);
LegacyNumericUtils.longToPrefixCoded(DateFormatUtil.parseMath(null, s).getTime(), 0, result);
break;
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + type);
@ -580,17 +580,17 @@ public class TrieField extends PrimitiveFieldType {
if (val != null) {
switch (type) {
case INTEGER:
LegacyNumericUtils.intToPrefixCodedBytes(val.intValue(), 0, bytes);
LegacyNumericUtils.intToPrefixCoded(val.intValue(), 0, bytes);
break;
case FLOAT:
LegacyNumericUtils.intToPrefixCodedBytes(LegacyNumericUtils.floatToSortableInt(val.floatValue()), 0, bytes);
LegacyNumericUtils.intToPrefixCoded(LegacyNumericUtils.floatToSortableInt(val.floatValue()), 0, bytes);
break;
case LONG: //fallthrough!
case DATE:
LegacyNumericUtils.longToPrefixCodedBytes(val.longValue(), 0, bytes);
LegacyNumericUtils.longToPrefixCoded(val.longValue(), 0, bytes);
break;
case DOUBLE:
LegacyNumericUtils.longToPrefixCodedBytes(LegacyNumericUtils.doubleToSortableLong(val.doubleValue()), 0, bytes);
LegacyNumericUtils.longToPrefixCoded(LegacyNumericUtils.doubleToSortableLong(val.doubleValue()), 0, bytes);
break;
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
@ -602,7 +602,7 @@ public class TrieField extends PrimitiveFieldType {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: "+f.name());
switch (type) {
case INTEGER:
LegacyNumericUtils.intToPrefixCodedBytes(toInt(bytesRef.bytes, bytesRef.offset), 0, bytes);
LegacyNumericUtils.intToPrefixCoded(toInt(bytesRef.bytes, bytesRef.offset), 0, bytes);
break;
case FLOAT: {
// WARNING: Code Duplication! Keep in sync with o.a.l.util.LegacyNumericUtils!
@ -610,12 +610,12 @@ public class TrieField extends PrimitiveFieldType {
// code in next 2 lines is identical to: int v = LegacyNumericUtils.floatToSortableInt(Float.intBitsToFloat(toInt(arr)));
int v = toInt(bytesRef.bytes, bytesRef.offset);
if (v<0) v ^= 0x7fffffff;
LegacyNumericUtils.intToPrefixCodedBytes(v, 0, bytes);
LegacyNumericUtils.intToPrefixCoded(v, 0, bytes);
break;
}
case LONG: //fallthrough!
case DATE:
LegacyNumericUtils.longToPrefixCodedBytes(toLong(bytesRef.bytes, bytesRef.offset), 0, bytes);
LegacyNumericUtils.longToPrefixCoded(toLong(bytesRef.bytes, bytesRef.offset), 0, bytes);
break;
case DOUBLE: {
// WARNING: Code Duplication! Keep in sync with o.a.l.util.LegacyNumericUtils!
@ -623,7 +623,7 @@ public class TrieField extends PrimitiveFieldType {
// code in next 2 lines is identical to: long v = LegacyNumericUtils.doubleToSortableLong(Double.longBitsToDouble(toLong(arr)));
long v = toLong(bytesRef.bytes, bytesRef.offset);
if (v<0) v ^= 0x7fffffffffffffffL;
LegacyNumericUtils.longToPrefixCodedBytes(v, 0, bytes);
LegacyNumericUtils.longToPrefixCoded(v, 0, bytes);
break;
}
default: