Example usage for org.apache.lucene.util BytesRef compareTo

List of usage examples for org.apache.lucene.util BytesRef compareTo

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRef compareTo.

Prototype

@Override
public int compareTo(BytesRef other) 

Source Link

Document

Unsigned byte order comparison

Usage

From source file:org.elasticsearch.index.fielddata.fieldcomparator.BytesRefOrdValComparator.java

License:Apache License

@Override
public void setBottom(final int bottom) {
    bottomSlot = bottom;//from w  w w. ja v a  2  s .  c  o  m
    final BytesRef bottomValue = values[bottomSlot];

    if (bottomValue == null) {
        bottomOrd = Ordinals.MISSING_ORDINAL;
    } else if (currentReaderGen == readerGen[bottomSlot]) {
        bottomOrd = ords[bottomSlot];
    } else {
        // insert an ord
        bottomOrd = ordInCurrentReader(termsIndex, bottomValue);
        if (bottomOrd == missingOrd) {
            // bottomValue and missingValue and in-between the same field data values -> tie-break
            // this is why we multiply ords by 4
            assert missingValue != null;
            final int cmp = bottomValue.compareTo(missingValue);
            if (cmp < 0) {
                --bottomOrd;
            } else if (cmp > 0) {
                ++bottomOrd;
            }
        }
        assert consistentInsertedOrd(termsIndex, bottomOrd, bottomValue);
    }
    readerGen[bottomSlot] = currentReaderGen;
}

From source file:org.elasticsearch.index.fielddata.fieldcomparator.BytesRefOrdValComparator.java

License:Apache License

final protected static long binarySearch(BytesValues.WithOrdinals a, BytesRef key, long low, long high) {
    assert low != Ordinals.MISSING_ORDINAL;
    assert high == Ordinals.MISSING_ORDINAL || (a.getValueByOrd(high) == null | a.getValueByOrd(high) != null); // make sure we actually can get these values
    assert low == high + 1 || a.getValueByOrd(low) == null | a.getValueByOrd(low) != null;
    while (low <= high) {
        long mid = (low + high) >>> 1;
        BytesRef midVal = a.getValueByOrd(mid);
        int cmp;/*from   w  w w .  ja v  a 2s .  c  o m*/
        if (midVal != null) {
            cmp = midVal.compareTo(key);
        } else {
            cmp = -1;
        }

        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return mid;
    }
    return -(low + 1);
}

From source file:org.elasticsearch.index.fielddata.fieldcomparator.BytesRefValComparator.java

License:Apache License

@Override
public int compareValues(BytesRef val1, BytesRef val2) {
    if (val1 == null) {
        if (val2 == null) {
            return 0;
        }//  ww w .j  a v  a 2  s.  co m
        return -1;
    } else if (val2 == null) {
        return 1;
    }
    return val1.compareTo(val2);
}

From source file:org.elasticsearch.index.fielddata.fieldcomparator.StringScriptDataComparator.java

License:Apache License

@Override
public int compare(int slot1, int slot2) {
    final BytesRef val1 = values[slot1];
    final BytesRef val2 = values[slot2];
    if (val1 == null) {
        if (val2 == null) {
            return 0;
        }// w w w.  jav a2 s .com
        return -1;
    } else if (val2 == null) {
        return 1;
    }

    return val1.compareTo(val2);
}

From source file:org.elasticsearch.index.fielddata.plain.AtomicFieldDataWithOrdinalsTermsEnum.java

License:Apache License

final private static long binarySearch(BytesValues.WithOrdinals a, BytesRef key) {
    long low = 1;
    long high = a.ordinals().getMaxOrd();
    while (low <= high) {
        long mid = (low + high) >>> 1;
        BytesRef midVal = a.getValueByOrd(mid);
        int cmp;/*w  ww  .ja  v a2 s  .  c o m*/
        if (midVal != null) {
            cmp = midVal.compareTo(key);
        } else {
            cmp = -1;
        }

        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return mid;
    }
    return -(low + 1);
}

From source file:org.elasticsearch.index.fielddata.plain.BytesValuesWithOrdinalsTermsEnum.java

License:Apache License

final private static long binarySearch(BytesValues.WithOrdinals a, BytesRef key) {
    long low = 1;
    long high = a.getMaxOrd();
    while (low <= high) {
        long mid = (low + high) >>> 1;
        BytesRef midVal = a.getValueByOrd(mid);
        int cmp;/*from  w ww .  j  a v a  2s .  com*/
        if (midVal != null) {
            cmp = midVal.compareTo(key);
        } else {
            cmp = -1;
        }

        if (cmp < 0)
            low = mid + 1;
        else if (cmp > 0)
            high = mid - 1;
        else
            return mid;
    }
    return -(low + 1);
}

From source file:org.elasticsearch.index.fielddata.plain.ParentChildIntersectTermsEnum.java

License:Apache License

@Override
public BytesRef next() throws IOException {
    if (states.isEmpty()) {
        return null;
    }/*from w  w  w.  ja v  a 2  s .  c  o m*/

    if (current == null) {
        // unpositioned
        for (TermsEnumState state : states) {
            state.initialize();
        }
    } else {
        int removed = 0;
        for (int i = 0; i < stateSlots.size(); i++) {
            int stateSlot = stateSlots.get(i);
            if (states.get(stateSlot - removed).next() == null) {
                states.remove(stateSlot - removed);
                removed++;
            }
        }

        if (states.isEmpty()) {
            return null;
        }
        stateSlots.clear();
    }

    BytesRef lowestTerm = states.get(0).term;
    stateSlots.add(0);
    for (int i = 1; i < states.size(); i++) {
        TermsEnumState state = states.get(i);
        int cmp = lowestTerm.compareTo(state.term);
        if (cmp > 0) {
            lowestTerm = state.term;
            stateSlots.clear();
            stateSlots.add(i);
        } else if (cmp == 0) {
            stateSlots.add(i);
        }
    }

    return current = lowestTerm;
}

From source file:org.elasticsearch.index.fielddata.plain.ParentChildIntersectTermsEnum.java

License:Apache License

@Override
public SeekStatus seekCeil(BytesRef text) throws IOException {
    if (states.isEmpty()) {
        return SeekStatus.END;
    }//from w w  w. j a v a2  s. co  m

    boolean found = false;
    if (current == null) {
        // unpositioned
        Iterator<TermsEnumState> iterator = states.iterator();
        while (iterator.hasNext()) {
            SeekStatus seekStatus = iterator.next().seekCeil(text);
            if (seekStatus == SeekStatus.END) {
                iterator.remove();
            } else if (seekStatus == SeekStatus.FOUND) {
                found = true;
            }
        }
    } else {
        int removed = 0;
        for (int i = 0; i < stateSlots.size(); i++) {
            int stateSlot = stateSlots.get(i);
            SeekStatus seekStatus = states.get(stateSlot - removed).seekCeil(text);
            if (seekStatus == SeekStatus.END) {
                states.remove(stateSlot - removed);
                removed++;
            } else if (seekStatus == SeekStatus.FOUND) {
                found = true;
            }
        }
    }

    if (states.isEmpty()) {
        return SeekStatus.END;
    }
    stateSlots.clear();

    if (found) {
        for (int i = 0; i < states.size(); i++) {
            if (states.get(i).term.equals(text)) {
                stateSlots.add(i);
            }
        }
        current = text;
        return SeekStatus.FOUND;
    } else {
        BytesRef lowestTerm = states.get(0).term;
        stateSlots.add(0);
        for (int i = 1; i < states.size(); i++) {
            TermsEnumState state = states.get(i);
            int cmp = lowestTerm.compareTo(state.term);
            if (cmp > 0) {
                lowestTerm = state.term;
                stateSlots.clear();
                stateSlots.add(i);
            } else if (cmp == 0) {
                stateSlots.add(i);
            }
        }
        current = lowestTerm;
        return SeekStatus.NOT_FOUND;
    }
}

From source file:org.elasticsearch.index.mapper.BinaryRangeUtilTests.java

License:Apache License

public void testBasics() {
    BytesRef encoded1 = new BytesRef(BinaryRangeUtil.encodeLong(Long.MIN_VALUE));
    BytesRef encoded2 = new BytesRef(BinaryRangeUtil.encodeLong(-1L));
    BytesRef encoded3 = new BytesRef(BinaryRangeUtil.encodeLong(0L));
    BytesRef encoded4 = new BytesRef(BinaryRangeUtil.encodeLong(1L));
    BytesRef encoded5 = new BytesRef(BinaryRangeUtil.encodeLong(Long.MAX_VALUE));

    assertTrue(encoded1.compareTo(encoded2) < 0);
    assertTrue(encoded2.compareTo(encoded1) > 0);
    assertTrue(encoded2.compareTo(encoded3) < 0);
    assertTrue(encoded3.compareTo(encoded2) > 0);
    assertTrue(encoded3.compareTo(encoded4) < 0);
    assertTrue(encoded4.compareTo(encoded3) > 0);
    assertTrue(encoded4.compareTo(encoded5) < 0);
    assertTrue(encoded5.compareTo(encoded4) > 0);

    encoded1 = new BytesRef(BinaryRangeUtil.encodeDouble(Double.NEGATIVE_INFINITY));
    encoded2 = new BytesRef(BinaryRangeUtil.encodeDouble(-1D));
    encoded3 = new BytesRef(BinaryRangeUtil.encodeDouble(-0D));
    encoded4 = new BytesRef(BinaryRangeUtil.encodeDouble(0D));
    encoded5 = new BytesRef(BinaryRangeUtil.encodeDouble(1D));
    BytesRef encoded6 = new BytesRef(BinaryRangeUtil.encodeDouble(Double.POSITIVE_INFINITY));

    assertTrue(encoded1.compareTo(encoded2) < 0);
    assertTrue(encoded2.compareTo(encoded1) > 0);
    assertTrue(encoded2.compareTo(encoded3) < 0);
    assertTrue(encoded3.compareTo(encoded2) > 0);
    assertTrue(encoded3.compareTo(encoded4) < 0);
    assertTrue(encoded4.compareTo(encoded3) > 0);
    assertTrue(encoded4.compareTo(encoded5) < 0);
    assertTrue(encoded5.compareTo(encoded4) > 0);
    assertTrue(encoded5.compareTo(encoded6) < 0);
    assertTrue(encoded6.compareTo(encoded5) > 0);

    encoded1 = new BytesRef(BinaryRangeUtil.encodeFloat(Float.NEGATIVE_INFINITY));
    encoded2 = new BytesRef(BinaryRangeUtil.encodeFloat(-1F));
    encoded3 = new BytesRef(BinaryRangeUtil.encodeFloat(-0F));
    encoded4 = new BytesRef(BinaryRangeUtil.encodeFloat(0F));
    encoded5 = new BytesRef(BinaryRangeUtil.encodeFloat(1F));
    encoded6 = new BytesRef(BinaryRangeUtil.encodeFloat(Float.POSITIVE_INFINITY));

    assertTrue(encoded1.compareTo(encoded2) < 0);
    assertTrue(encoded2.compareTo(encoded1) > 0);
    assertTrue(encoded2.compareTo(encoded3) < 0);
    assertTrue(encoded3.compareTo(encoded2) > 0);
    assertTrue(encoded3.compareTo(encoded4) < 0);
    assertTrue(encoded4.compareTo(encoded3) > 0);
    assertTrue(encoded4.compareTo(encoded5) < 0);
    assertTrue(encoded5.compareTo(encoded4) > 0);
    assertTrue(encoded5.compareTo(encoded6) < 0);
    assertTrue(encoded6.compareTo(encoded5) > 0);
}

From source file:org.elasticsearch.index.mapper.BinaryRangeUtilTests.java

License:Apache License

public void testEncode_long() {
    int iters = randomIntBetween(32, 1024);
    for (int i = 0; i < iters; i++) {
        long number1 = randomLong();
        BytesRef encodedNumber1 = new BytesRef(BinaryRangeUtil.encodeLong(number1));
        long number2 = randomBoolean() ? number1 + 1 : randomLong();
        BytesRef encodedNumber2 = new BytesRef(BinaryRangeUtil.encodeLong(number2));

        int cmp = normalize(Long.compare(number1, number2));
        assertEquals(cmp, normalize(encodedNumber1.compareTo(encodedNumber2)));
        cmp = normalize(Long.compare(number2, number1));
        assertEquals(cmp, normalize(encodedNumber2.compareTo(encodedNumber1)));
    }/*from  ww  w  . ja v a 2  s  . c  o  m*/
}