Java Code Examples for java.util.Arrays#compareUnsigned()

The following examples show how to use java.util.Arrays#compareUnsigned() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LatLonShapeBoundingBoxQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * static utility method to compare a bbox with a range of triangles (just the bbox of the triangle collection)
 **/
private static Relation compareBBoxToRangeBBox(final byte[] bbox,
                                               int minXOffset, int minYOffset, byte[] minTriangle,
                                               int maxXOffset, int maxYOffset, byte[] maxTriangle) {
  // check bounding box (DISJOINT)
  if (disjoint(bbox, minXOffset, minYOffset, minTriangle, maxXOffset, maxYOffset, maxTriangle)) {
    return Relation.CELL_OUTSIDE_QUERY;
  }

  if (Arrays.compareUnsigned(minTriangle, minXOffset, minXOffset + BYTES, bbox, BYTES, 2 * BYTES) >= 0 &&
      Arrays.compareUnsigned(maxTriangle, maxXOffset, maxXOffset + BYTES, bbox, 3 * BYTES, 4 * BYTES) <= 0 &&
      Arrays.compareUnsigned(minTriangle, minYOffset, minYOffset + BYTES, bbox, 0, BYTES) >= 0 &&
      Arrays.compareUnsigned(maxTriangle, maxYOffset, maxYOffset + BYTES, bbox, 2 * BYTES, 3 * BYTES) <= 0) {
    return Relation.CELL_INSIDE_QUERY;
  }

  return Relation.CELL_CROSSES_QUERY;
}
 
Example 2
Source File: TestBKDRadixSelector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private byte[] getMinDataDimension(BKDRadixSelector.PathSlice p, int bytesPerDimension, int dataDims, int indexDims, byte[] minDim, int splitDim) throws  IOException {
  byte[] min = new byte[(dataDims - indexDims) * bytesPerDimension];
  Arrays.fill(min, (byte) 0xff);
  int offset = splitDim * bytesPerDimension;
  try (PointReader reader = p.writer.getReader(p.start, p.count)) {
    byte[] value = new byte[(dataDims - indexDims) * bytesPerDimension];
    while (reader.next()) {
      PointValue pointValue = reader.pointValue();
      BytesRef packedValue = pointValue.packedValue();
      if (Arrays.mismatch(minDim, 0, bytesPerDimension, packedValue.bytes, packedValue.offset + offset, packedValue.offset + offset + bytesPerDimension) == -1) {
        System.arraycopy(packedValue.bytes, packedValue.offset + indexDims * bytesPerDimension, value, 0, (dataDims - indexDims) * bytesPerDimension);
        if (Arrays.compareUnsigned(min, 0, (dataDims - indexDims) * bytesPerDimension, value, 0, (dataDims - indexDims) * bytesPerDimension) > 0) {
          System.arraycopy(value, 0, min, 0, (dataDims - indexDims) * bytesPerDimension);
        }
      }
    }
  }
  return min;
}
 
Example 3
Source File: BKDWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private boolean valueInOrder(long ord, int sortedDim, byte[] lastPackedValue, byte[] packedValue, int packedValueOffset,
    int doc, int lastDoc) {
  int dimOffset = sortedDim * bytesPerDim;
  if (ord > 0) {
    int cmp = Arrays.compareUnsigned(lastPackedValue, dimOffset, dimOffset + bytesPerDim, packedValue, packedValueOffset + dimOffset, packedValueOffset + dimOffset + bytesPerDim);
    if (cmp > 0) {
      throw new AssertionError("values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue, packedValueOffset, packedBytesLength) + " ord=" + ord);
    }
    if (cmp == 0  && numDataDims > numIndexDims) {
      int dataOffset = numIndexDims * bytesPerDim;
      cmp = Arrays.compareUnsigned(lastPackedValue, dataOffset, packedBytesLength, packedValue, packedValueOffset + dataOffset, packedValueOffset + packedBytesLength);
      if (cmp > 0) {
        throw new AssertionError("data values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue, packedValueOffset, packedBytesLength) + " ord=" + ord);
      }
    }
    if (cmp == 0 && doc < lastDoc) {
      throw new AssertionError("docs out of order: last doc=" + lastDoc + " current doc=" + doc + " ord=" + ord);
    }
  }
  System.arraycopy(packedValue, packedValueOffset, lastPackedValue, 0, packedBytesLength);
  return true;
}
 
Example 4
Source File: TestBKDRadixSelector.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private int getMinDocId(BKDRadixSelector.PathSlice p, int bytesPerDimension, int dimension, byte[] partitionPoint, int dataDims, int indexDims, byte[] dataDim) throws  IOException {
 int docID = Integer.MAX_VALUE;
  try (PointReader reader = p.writer.getReader(p.start, p.count)) {
    while (reader.next()) {
      PointValue pointValue = reader.pointValue();
      BytesRef packedValue = pointValue.packedValue();
      int offset = dimension * bytesPerDimension;
      int dataOffset = indexDims * bytesPerDimension;
      int dataLength = (dataDims - indexDims) * bytesPerDimension;
      if (Arrays.compareUnsigned(packedValue.bytes, packedValue.offset + offset, packedValue.offset + offset + bytesPerDimension, partitionPoint, 0, bytesPerDimension) == 0
        && Arrays.compareUnsigned(packedValue.bytes, packedValue.offset + dataOffset, packedValue.offset + dataOffset + dataLength, dataDim, 0, dataLength) == 0) {
        int newDocID = pointValue.docID();
        if (newDocID < docID) {
          docID = newDocID;
        }
      }
    }
  }
  return docID;
}
 
Example 5
Source File: SimpleTextBKDWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private boolean valueInOrder(long ord, int sortedDim, byte[] lastPackedValue, byte[] packedValue, int packedValueOffset,
    int doc, int lastDoc) {
  int dimOffset = sortedDim * bytesPerDim;
  if (ord > 0) {
    int cmp = Arrays.compareUnsigned(lastPackedValue, dimOffset, dimOffset + bytesPerDim, packedValue, packedValueOffset + dimOffset, packedValueOffset + dimOffset + bytesPerDim);
    if (cmp > 0) {
      throw new AssertionError("values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue, packedValueOffset, packedBytesLength) + " ord=" + ord + " sortedDim=" + sortedDim);
    }
    if (cmp == 0  && numDataDims > numIndexDims) {
      int dataOffset = numIndexDims * bytesPerDim;
      cmp = Arrays.compareUnsigned(lastPackedValue, dataOffset, packedBytesLength, packedValue, packedValueOffset + dataOffset, packedValueOffset + packedBytesLength);
      if (cmp > 0) {
        throw new AssertionError("data values out of order: last value=" + new BytesRef(lastPackedValue) + " current value=" + new BytesRef(packedValue, packedValueOffset, packedBytesLength) + " ord=" + ord);
      }
    }
    if (cmp == 0 && doc < lastDoc) {
      throw new AssertionError("docs out of order: last doc=" + lastDoc + " current doc=" + doc + " ord=" + ord + " sortedDim=" + sortedDim);
    }
  }
  System.arraycopy(packedValue, packedValueOffset, lastPackedValue, 0, packedBytesLength);
  return true;
}
 
Example 6
Source File: RangeFieldQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
Relation compare(byte[] queryPackedValue, byte[] minPackedValue, byte[] maxPackedValue,
    int numDims, int bytesPerDim, int dim) {
  int minOffset = dim * bytesPerDim;
  int maxOffset = minOffset + bytesPerDim * numDims;

  if (Arrays.compareUnsigned(queryPackedValue, maxOffset, maxOffset + bytesPerDim, maxPackedValue, maxOffset, maxOffset + bytesPerDim) > 0
      || Arrays.compareUnsigned(queryPackedValue, minOffset, minOffset + bytesPerDim, minPackedValue, minOffset, minOffset + bytesPerDim) < 0) {
    // all ranges are either less than the query max or greater than the query min
    return Relation.CELL_OUTSIDE_QUERY;
  }

  if (Arrays.compareUnsigned(queryPackedValue, maxOffset, maxOffset + bytesPerDim, minPackedValue, maxOffset, maxOffset + bytesPerDim) <= 0
      && Arrays.compareUnsigned(queryPackedValue, minOffset, minOffset + bytesPerDim, maxPackedValue, minOffset, minOffset + bytesPerDim) >= 0) {
    return Relation.CELL_INSIDE_QUERY;
  }

  return Relation.CELL_CROSSES_QUERY;
}
 
Example 7
Source File: TestInetAddressRangeQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void setMax(int dim, Object val) {
  InetAddress v = (InetAddress)val;
  byte[] e = InetAddressPoint.encode(v);

  if (Arrays.compareUnsigned(max, 0, e.length, e, 0, e.length) > 0) {
    min = e;
    minAddress = v;
  } else {
    max = e;
    maxAddress = v;
  }
}
 
Example 8
Source File: PointValues.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Return the maximum packed values across all leaves of the given
 * {@link IndexReader}. Leaves that do not have points for the given field
 * are ignored.
 *  @see PointValues#getMaxPackedValue() */
public static byte[] getMaxPackedValue(IndexReader reader, String field) throws IOException {
  byte[] maxValue = null;
  for (LeafReaderContext ctx : reader.leaves()) {
    PointValues values = ctx.reader().getPointValues(field);
    if (values == null) {
      continue;
    }
    byte[] leafMaxValue = values.getMaxPackedValue();
    if (leafMaxValue == null) {
      continue;
    }
    if (maxValue == null) {
      maxValue = leafMaxValue.clone();
    } else {
      final int numDimensions = values.getNumIndexDimensions();
      final int numBytesPerDimension = values.getBytesPerDimension();
      for (int i = 0; i < numDimensions; ++i) {
        int offset = i * numBytesPerDimension;
        if (Arrays.compareUnsigned(leafMaxValue, offset, offset + numBytesPerDimension, maxValue, offset, offset + numBytesPerDimension) > 0) {
          System.arraycopy(leafMaxValue, offset, maxValue, offset, numBytesPerDimension);
        }
      }
    }
  }
  return maxValue;
}
 
Example 9
Source File: CheckIndex.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public PointValues.Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {
  checkPackedValue("min packed value", minPackedValue, -1);
  System.arraycopy(minPackedValue, 0, lastMinPackedValue, 0, packedIndexBytesCount);
  checkPackedValue("max packed value", maxPackedValue, -1);
  System.arraycopy(maxPackedValue, 0, lastMaxPackedValue, 0, packedIndexBytesCount);

  for(int dim=0;dim<numIndexDims;dim++) {
    int offset = bytesPerDim * dim;

    if (Arrays.compareUnsigned(minPackedValue, offset, offset + bytesPerDim, maxPackedValue, offset, offset + bytesPerDim) > 0) {
      throw new RuntimeException("packed points cell minPackedValue " + Arrays.toString(minPackedValue) +
                                 " is out-of-bounds of the cell's maxPackedValue " + Arrays.toString(maxPackedValue) + " dim=" + dim + " field=\"" + fieldName + "\"");
    }

    // Make sure this cell is not outside of the global min/max:
    if (Arrays.compareUnsigned(minPackedValue, offset, offset + bytesPerDim, globalMinPackedValue, offset, offset + bytesPerDim) < 0) {
      throw new RuntimeException("packed points cell minPackedValue " + Arrays.toString(minPackedValue) +
                                 " is out-of-bounds of the global minimum " + Arrays.toString(globalMinPackedValue) + " dim=" + dim + " field=\"" + fieldName + "\"");
    }

    if (Arrays.compareUnsigned(maxPackedValue, offset, offset + bytesPerDim, globalMinPackedValue, offset, offset + bytesPerDim) < 0) {
      throw new RuntimeException("packed points cell maxPackedValue " + Arrays.toString(maxPackedValue) +
                                 " is out-of-bounds of the global minimum " + Arrays.toString(globalMinPackedValue) + " dim=" + dim + " field=\"" + fieldName + "\"");
    }

    if (Arrays.compareUnsigned(minPackedValue, offset, offset + bytesPerDim, globalMaxPackedValue, offset, offset + bytesPerDim) > 0) {
      throw new RuntimeException("packed points cell minPackedValue " + Arrays.toString(minPackedValue) +
                                 " is out-of-bounds of the global maximum " + Arrays.toString(globalMaxPackedValue) + " dim=" + dim + " field=\"" + fieldName + "\"");
    }
    if (Arrays.compareUnsigned(maxPackedValue, offset, offset + bytesPerDim, globalMaxPackedValue, offset, offset + bytesPerDim) > 0) {
      throw new RuntimeException("packed points cell maxPackedValue " + Arrays.toString(maxPackedValue) +
                                 " is out-of-bounds of the global maximum " + Arrays.toString(globalMaxPackedValue) + " dim=" + dim + " field=\"" + fieldName + "\"");
    }
  }                                   

  // We always pretend the query shape is so complex that it crosses every cell, so
  // that packedValue is passed for every document
  return PointValues.Relation.CELL_CROSSES_QUERY;
}
 
Example 10
Source File: PointInSetQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {

  boolean crosses = false;

  for(int dim=0;dim<numDims;dim++) {
    int offset = dim*bytesPerDim;

    int cmpMin = Arrays.compareUnsigned(minPackedValue, offset, offset + bytesPerDim, pointBytes, offset, offset + bytesPerDim);
    if (cmpMin > 0) {
      return Relation.CELL_OUTSIDE_QUERY;
    }

    int cmpMax = Arrays.compareUnsigned(maxPackedValue, offset, offset + bytesPerDim, pointBytes, offset, offset + bytesPerDim);
    if (cmpMax < 0) {
      return Relation.CELL_OUTSIDE_QUERY;
    }

    if (cmpMin != 0 || cmpMax != 0) {
      crosses = true;
    }
  }

  if (crosses) {
    return Relation.CELL_CROSSES_QUERY;
  } else {
    // NOTE: we only hit this if we are on a cell whose min and max values are exactly equal to our point,
    // which can easily happen if many docs share this one value
    return Relation.CELL_INSIDE_QUERY;
  }
}
 
Example 11
Source File: PointsStackTracker.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public boolean contains(Cell other) {
  for(int dim=0;dim<numDims;dim++) {
    int offset = dim * bytesPerDim;
    // other.min < min?
    if (Arrays.compareUnsigned(other.minPackedValue, offset, offset + bytesPerDim, minPackedValue, offset, offset + bytesPerDim) < 0) {
      return false;
    }
    // other.max > max?
    if (Arrays.compareUnsigned(other.maxPackedValue, offset, offset + bytesPerDim, maxPackedValue, offset, offset + bytesPerDim) > 0) {
      return false;
    }
  }

  return true;
}
 
Example 12
Source File: GraphTermsQParserPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public PointValues.Relation compare(byte[] minPackedValue, byte[] maxPackedValue) {

  boolean crosses = false;

  for(int dim=0;dim<numDims;dim++) {
    int offset = dim*bytesPerDim;

    int cmpMin = Arrays.compareUnsigned(minPackedValue, offset, offset + bytesPerDim, pointBytes, offset, offset + bytesPerDim);
    if (cmpMin > 0) {
      return PointValues.Relation.CELL_OUTSIDE_QUERY;
    }

    int cmpMax = Arrays.compareUnsigned(maxPackedValue, offset, offset + bytesPerDim, pointBytes, offset, offset + bytesPerDim);
    if (cmpMax < 0) {
      return PointValues.Relation.CELL_OUTSIDE_QUERY;
    }

    if (cmpMin != 0 || cmpMax != 0) {
      crosses = true;
    }
  }

  if (crosses) {
    return PointValues.Relation.CELL_CROSSES_QUERY;
  } else {
    // NOTE: we only hit this if we are on a cell whose min and max values are exactly equal to our point,
    // which can easily happen if many docs share this one value
    return PointValues.Relation.CELL_INSIDE_QUERY;
  }
}
 
Example 13
Source File: BKDWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Called only in assert */
private boolean valueInBounds(BytesRef packedValue, byte[] minPackedValue, byte[] maxPackedValue) {
  for(int dim=0;dim<numIndexDims;dim++) {
    int offset = bytesPerDim*dim;
    if (Arrays.compareUnsigned(packedValue.bytes, packedValue.offset + offset, packedValue.offset + offset + bytesPerDim, minPackedValue, offset, offset + bytesPerDim) < 0) {
      return false;
    }
    if (Arrays.compareUnsigned(packedValue.bytes, packedValue.offset + offset, packedValue.offset + offset + bytesPerDim, maxPackedValue, offset, offset + bytesPerDim) > 0) {
      return false;
    }
  }

  return true;
}
 
Example 14
Source File: TestInetAddressRangeQueries.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
protected void setMin(int dim, Object val) {
  InetAddress v = (InetAddress)val;
  byte[] e = InetAddressPoint.encode(v);

  if (Arrays.compareUnsigned(min, 0, e.length, e, 0, e.length) < 0) {
    max = e;
    maxAddress = v;
  } else {
    min = e;
    minAddress = v;
  }
}
 
Example 15
Source File: LatLonShapeBoundingBoxQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * static utility method to compare a bbox with a range of triangles (just the bbox of the triangle collection)
 * for intersection
 **/
private static Relation intersectBBoxWithRangeBBox(final byte[] bbox,
                                                   int minXOffset, int minYOffset, byte[] minTriangle,
                                                   int maxXOffset, int maxYOffset, byte[] maxTriangle) {
  // check bounding box (DISJOINT)
  if (disjoint(bbox, minXOffset, minYOffset, minTriangle, maxXOffset, maxYOffset, maxTriangle)) {
    return Relation.CELL_OUTSIDE_QUERY;
  }

  if (Arrays.compareUnsigned(minTriangle, minXOffset, minXOffset + BYTES, bbox, BYTES, 2 * BYTES) >= 0 &&
      Arrays.compareUnsigned(minTriangle, minYOffset, minYOffset + BYTES, bbox, 0, BYTES) >= 0) {
    if (Arrays.compareUnsigned(maxTriangle, minXOffset, minXOffset + BYTES, bbox, 3 * BYTES, 4 * BYTES) <= 0 &&
        Arrays.compareUnsigned(maxTriangle, maxYOffset, maxYOffset + BYTES, bbox, 2 * BYTES, 3 * BYTES) <= 0) {
      return Relation.CELL_INSIDE_QUERY;
    }
    if (Arrays.compareUnsigned(maxTriangle, maxXOffset, maxXOffset + BYTES, bbox, 3 * BYTES, 4 * BYTES) <= 0 &&
        Arrays.compareUnsigned(maxTriangle, minYOffset, minYOffset + BYTES, bbox, 2 * BYTES, 3 * BYTES) <= 0) {
      return Relation.CELL_INSIDE_QUERY;
    }
  }

  if (Arrays.compareUnsigned(maxTriangle, maxXOffset, maxXOffset + BYTES, bbox, 3 * BYTES, 4 * BYTES) <= 0 &&
      Arrays.compareUnsigned(maxTriangle, maxYOffset, maxYOffset + BYTES, bbox, 2 * BYTES, 3 * BYTES) <= 0) {
    if (Arrays.compareUnsigned(minTriangle, minXOffset, minXOffset + BYTES, bbox, BYTES, 2 * BYTES) >= 0 &&
        Arrays.compareUnsigned(minTriangle, maxYOffset, maxYOffset + BYTES, bbox, 0, BYTES) >= 0) {
      return Relation.CELL_INSIDE_QUERY;
    }
    if (Arrays.compareUnsigned(minTriangle, maxXOffset, maxXOffset + BYTES, bbox, BYTES, 2 * BYTES) >= 0 &&
        Arrays.compareUnsigned(minTriangle, minYOffset, minYOffset + BYTES, bbox, 0, BYTES) >= 0) {
      return Relation.CELL_INSIDE_QUERY;
    }
  }

  return Relation.CELL_CROSSES_QUERY;
}
 
Example 16
Source File: InetAddressRange.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** encode the min/max range into the provided byte array */
private static void encode(final InetAddress min, final InetAddress max, final byte[] bytes) {
  // encode min and max value (consistent w/ InetAddressPoint encoding)
  final byte[] minEncoded = InetAddressPoint.encode(min);
  final byte[] maxEncoded = InetAddressPoint.encode(max);
  // ensure min is lt max
  if (Arrays.compareUnsigned(minEncoded, 0, BYTES, maxEncoded, 0, BYTES) > 0) {
    throw new IllegalArgumentException("min value cannot be greater than max value for InetAddressRange field");
  }
  System.arraycopy(minEncoded, 0, bytes, 0, BYTES);
  System.arraycopy(maxEncoded, 0, bytes, BYTES, BYTES);
}
 
Example 17
Source File: SimpleTextBKDWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected int split(byte[] minPackedValue, byte[] maxPackedValue) {
  // Find which dim has the largest span so we can split on it:
  int splitDim = -1;
  for(int dim=0;dim<numIndexDims;dim++) {
    NumericUtils.subtract(bytesPerDim, dim, maxPackedValue, minPackedValue, scratchDiff);
    if (splitDim == -1 || Arrays.compareUnsigned(scratchDiff, 0, bytesPerDim, scratch1, 0, bytesPerDim) > 0) {
      System.arraycopy(scratchDiff, 0, scratch1, 0, bytesPerDim);
      splitDim = dim;
    }
  }

  //System.out.println("SPLIT: " + splitDim);
  return splitDim;
}
 
Example 18
Source File: CompareBytesUtils.java    From herddb with Apache License 2.0 5 votes vote down vote up
public static int compare(
        byte[] left, int fromIndex, int toIndex,
        byte[] right, int fromIndex2, int toIndex2
) {
    return Arrays.compareUnsigned(left, fromIndex, toIndex,
            right, fromIndex2, toIndex2);
}
 
Example 19
Source File: TestInetAddressRangeQueries.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
protected boolean isWithin(Range o) {
  IpRange other = (IpRange)o;
  return Arrays.compareUnsigned(min, 0, min.length, other.min, 0, min.length) >= 0 &&
      Arrays.compareUnsigned(max, 0, max.length, other.max, 0, max.length) <= 0;
}
 
Example 20
Source File: CompareBytesUtils.java    From herddb with Apache License 2.0 4 votes vote down vote up
public static int compare(byte[] left, byte[] right) {
    return Arrays.compareUnsigned(left, right);
}