From 6261767b336c37b1211aded44e7c8589f60b052c Mon Sep 17 00:00:00 2001 From: Mike McCandless Date: Tue, 1 Mar 2016 14:32:08 -0500 Subject: [PATCH] remove dup'd code --- .../org/apache/lucene/util/NumericUtils.java | 21 +++---------------- .../org/apache/lucene/util/bkd/BKDReader.java | 3 +-- .../org/apache/lucene/util/bkd/BKDWriter.java | 15 ++++++------- .../lucene/search/TestPointQueries.java | 6 +++--- .../org/apache/lucene/util/bkd/TestBKD.java | 19 +++++++++-------- .../lucene/index/BasePointFormatTestCase.java | 18 ++++++++-------- 6 files changed, 34 insertions(+), 48 deletions(-) diff --git a/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java b/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java index ee7ed3b24eb..b2373282828 100644 --- a/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java +++ b/lucene/core/src/java/org/apache/lucene/util/NumericUtils.java @@ -122,32 +122,17 @@ public final class NumericUtils { } } - /** Returns positive int if a > b, negative int if a < b and 0 if a == b */ - public static int compare(int bytesPerDim, byte[] a, int aIndex, byte[] b, int bIndex) { - assert aIndex >= 0; - assert bIndex >= 0; - int aOffset = aIndex*bytesPerDim; - int bOffset = bIndex*bytesPerDim; - for(int i=0;i 0) { + int offset = dim * bytesPerDim; + if (StringHelper.compare(bytesPerDim, minPackedA, offset, minPackedB, offset) > 0) { return false; } - if (compare(bytesPerDim, maxPackedA, dim, maxPackedB, dim) < 0) { + if (StringHelper.compare(bytesPerDim, maxPackedA, offset, maxPackedB, offset) < 0) { return false; } } diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java index 1067f4baf19..9d7259969af 100644 --- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java +++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDReader.java @@ -25,7 +25,6 @@ import org.apache.lucene.index.PointValues.Relation; import org.apache.lucene.store.IndexInput; import org.apache.lucene.util.Accountable; import org.apache.lucene.util.BytesRef; -import org.apache.lucene.util.NumericUtils; import org.apache.lucene.util.StringHelper; /** Handles intersection of an multi-dimensional shape in byte[] space with a block KD-tree previously written with {@link BKDWriter}. @@ -175,7 +174,7 @@ public class BKDReader implements Accountable { // With only 1D, all values should always be in sorted order if (lastPackedValue == null) { lastPackedValue = Arrays.copyOf(packedValue, packedValue.length); - } else if (NumericUtils.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) { + } else if (StringHelper.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) { throw new RuntimeException("value=" + new BytesRef(packedValue) + " for docID=" + docID + " dim=0" + " sorts before last value=" + new BytesRef(lastPackedValue)); } else { System.arraycopy(packedValue, 0, lastPackedValue, 0, bytesPerDim); diff --git a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java index 62c5264ec81..d7e19543f5c 100644 --- a/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java +++ b/lucene/core/src/java/org/apache/lucene/util/bkd/BKDWriter.java @@ -574,7 +574,7 @@ public class BKDWriter implements Closeable { int block = j / writer.valuesPerBlock; int index = j % writer.valuesPerBlock; assert index >= 0: "index=" + index + " j=" + j; - int cmp = NumericUtils.compare(bytesPerDim, pivotPackedValue, 0, writer.blocks.get(block), index*numDims+dim); + int cmp = StringHelper.compare(bytesPerDim, pivotPackedValue, 0, writer.blocks.get(block), bytesPerDim*(index*numDims+dim)); if (cmp != 0) { return cmp; } @@ -618,7 +618,7 @@ public class BKDWriter implements Closeable { int dimI = i % writer.valuesPerBlock; int blockJ = j / writer.valuesPerBlock; int dimJ = j % writer.valuesPerBlock; - int cmp = NumericUtils.compare(bytesPerDim, writer.blocks.get(blockI), dimI*numDims+dim, writer.blocks.get(blockJ), dimJ*numDims+dim); + int cmp = StringHelper.compare(bytesPerDim, writer.blocks.get(blockI), bytesPerDim*(dimI*numDims+dim), writer.blocks.get(blockJ), bytesPerDim*(dimJ*numDims+dim)); if (cmp != 0) { return cmp; } @@ -681,7 +681,7 @@ public class BKDWriter implements Closeable { final int docIDB = reader.readVInt(); final long ordB = reader.readVLong(); - int cmp = NumericUtils.compare(bytesPerDim, scratch1, dim, scratch2, dim); + int cmp = StringHelper.compare(bytesPerDim, scratch1, bytesPerDim*dim, scratch2, bytesPerDim*dim); if (cmp != 0) { return cmp; @@ -967,10 +967,11 @@ public class BKDWriter implements Closeable { /** Called only in assert */ private boolean valueInBounds(byte[] packedValue, byte[] minPackedValue, byte[] maxPackedValue) { for(int dim=0;dim 0) { + if (StringHelper.compare(bytesPerDim, packedValue, offset, maxPackedValue, offset) > 0) { return false; } } @@ -984,7 +985,7 @@ public class BKDWriter implements Closeable { int splitDim = -1; for(int dim=0;dim 0) { + if (splitDim == -1 || StringHelper.compare(bytesPerDim, scratchDiff, 0, scratch1, 0) > 0) { System.arraycopy(scratchDiff, 0, scratch1, 0, bytesPerDim); splitDim = dim; } @@ -1194,7 +1195,7 @@ public class BKDWriter implements Closeable { // only called from assert private boolean valueInOrder(long ord, byte[] lastPackedValue, byte[] packedValue) { - if (ord > 0 && NumericUtils.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) { + if (ord > 0 && StringHelper.compare(bytesPerDim, lastPackedValue, 0, packedValue, 0) > 0) { throw new AssertionError("values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue) + " ord=" + ord); } System.arraycopy(packedValue, 0, lastPackedValue, 0, bytesPerDim); diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java index ef5af2bec55..8a578fa5779 100644 --- a/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java +++ b/lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java @@ -678,7 +678,7 @@ public class TestPointQueries extends LuceneTestCase { // open-ended on the upper bound } - if (lower[dim] != null && upper[dim] != null && NumericUtils.compare(bytesPerDim, lower[dim], 0, upper[dim], 0) > 0) { + if (lower[dim] != null && upper[dim] != null && StringHelper.compare(bytesPerDim, lower[dim], 0, upper[dim], 0) > 0) { byte[] x = lower[dim]; lower[dim] = upper[dim]; upper[dim] = x; @@ -797,7 +797,7 @@ public class TestPointQueries extends LuceneTestCase { if (lower[dim] == null) { cmp = 1; } else { - cmp = NumericUtils.compare(bytesPerDim, value[dim], 0, lower[dim], 0); + cmp = StringHelper.compare(bytesPerDim, value[dim], 0, lower[dim], 0); } if (cmp < 0 || (cmp == 0 && includeLower[dim] == false)) { @@ -808,7 +808,7 @@ public class TestPointQueries extends LuceneTestCase { if (upper[dim] == null) { cmp = -1; } else { - cmp = NumericUtils.compare(bytesPerDim, value[dim], 0, upper[dim], 0); + cmp = StringHelper.compare(bytesPerDim, value[dim], 0, upper[dim], 0); } if (cmp > 0 || (cmp == 0 && includeUpper[dim] == false)) { diff --git a/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java b/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java index f24b805bea6..1688e6f0301 100644 --- a/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java +++ b/lucene/core/src/test/org/apache/lucene/util/bkd/TestBKD.java @@ -35,6 +35,7 @@ import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.LuceneTestCase; import org.apache.lucene.util.NumericUtils; +import org.apache.lucene.util.StringHelper; import org.apache.lucene.util.TestUtil; public class TestBKD extends LuceneTestCase { @@ -734,7 +735,7 @@ public class TestBKD extends LuceneTestCase { random().nextBytes(queryMin[dim]); queryMax[dim] = new byte[numBytesPerDim]; random().nextBytes(queryMax[dim]); - if (NumericUtils.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) { + if (StringHelper.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) { byte[] x = queryMin[dim]; queryMin[dim] = queryMax[dim]; queryMax[dim] = x; @@ -753,8 +754,8 @@ public class TestBKD extends LuceneTestCase { public void visit(int docID, byte[] packedValue) { //System.out.println("visit check docID=" + docID); for(int dim=0;dim 0) { + if (StringHelper.compare(numBytesPerDim, packedValue, dim*numBytesPerDim, queryMin[dim], 0) < 0 || + StringHelper.compare(numBytesPerDim, packedValue, dim*numBytesPerDim, queryMax[dim], 0) > 0) { //System.out.println(" no"); return; } @@ -768,11 +769,11 @@ public class TestBKD extends LuceneTestCase { public Relation compare(byte[] minPacked, byte[] maxPacked) { boolean crosses = false; for(int dim=0;dim 0) { + if (StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 || + StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) { return Relation.CELL_OUTSIDE_QUERY; - } else if (NumericUtils.compare(numBytesPerDim, minPacked, dim, queryMin[dim], 0) < 0 || - NumericUtils.compare(numBytesPerDim, maxPacked, dim, queryMax[dim], 0) > 0) { + } else if (StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 || + StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) { crosses = true; } } @@ -790,8 +791,8 @@ public class TestBKD extends LuceneTestCase { boolean matches = true; for(int dim=0;dim 0) { + if (StringHelper.compare(numBytesPerDim, x, 0, queryMin[dim], 0) < 0 || + StringHelper.compare(numBytesPerDim, x, 0, queryMax[dim], 0) > 0) { matches = false; break; } diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/BasePointFormatTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/index/BasePointFormatTestCase.java index 3f61bd1aa52..1a81138e3e1 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/BasePointFormatTestCase.java +++ b/lucene/test-framework/src/java/org/apache/lucene/index/BasePointFormatTestCase.java @@ -701,7 +701,7 @@ public abstract class BasePointFormatTestCase extends BaseIndexFileFormatTestCas random().nextBytes(queryMin[dim]); queryMax[dim] = new byte[numBytesPerDim]; random().nextBytes(queryMax[dim]); - if (NumericUtils.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) { + if (StringHelper.compare(numBytesPerDim, queryMin[dim], 0, queryMax[dim], 0) > 0) { byte[] x = queryMin[dim]; queryMin[dim] = queryMax[dim]; queryMax[dim] = x; @@ -733,8 +733,8 @@ public abstract class BasePointFormatTestCase extends BaseIndexFileFormatTestCas //System.out.println("visit check docID=" + docID + " id=" + idValues.get(docID)); for(int dim=0;dim 0) { + if (StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 || + StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) { //System.out.println(" query_outside_cell"); return Relation.CELL_OUTSIDE_QUERY; - } else if (NumericUtils.compare(numBytesPerDim, minPacked, dim, queryMin[dim], 0) < 0 || - NumericUtils.compare(numBytesPerDim, maxPacked, dim, queryMax[dim], 0) > 0) { + } else if (StringHelper.compare(numBytesPerDim, minPacked, dim*numBytesPerDim, queryMin[dim], 0) < 0 || + StringHelper.compare(numBytesPerDim, maxPacked, dim*numBytesPerDim, queryMax[dim], 0) > 0) { crosses = true; } } @@ -774,8 +774,8 @@ public abstract class BasePointFormatTestCase extends BaseIndexFileFormatTestCas boolean matches = true; for(int dim=0;dim 0) { + if (StringHelper.compare(numBytesPerDim, x, 0, queryMin[dim], 0) < 0 || + StringHelper.compare(numBytesPerDim, x, 0, queryMax[dim], 0) > 0) { matches = false; break; }