Example usage for org.apache.lucene.util BytesRefBuilder setLength

List of usage examples for org.apache.lucene.util BytesRefBuilder setLength

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRefBuilder setLength.

Prototype

public void setLength(int length) 

Source Link

Document

Set the length.

Usage

From source file:org.apache.solr.codecs.onsql.ONSQLUtil.java

License:Apache License

public static void readLine(DataInput in, BytesRefBuilder scratch) throws IOException {
    int upto = 0;
    while (true) {
        byte b = in.readByte();
        scratch.grow(1 + upto);/*ww w . j a v a  2s. co m*/
        if (b == ESCAPE) {
            scratch.setByteAt(upto++, in.readByte());
        } else {
            if (b == NEWLINE) {
                break;
            } else {
                scratch.setByteAt(upto++, b);
            }
        }
    }
    scratch.setLength(upto);
}

From source file:org.apache.solr.legacy.LegacyNumericUtils.java

License:Apache License

/**
 * Returns prefix coded bits after reducing the precision by <code>shift</code> bits.
 * This is method is used by {@link org.apache.solr.legacy.LegacyNumericTokenStream}.
 * After encoding, {@code bytes.offset} will always be 0. 
 * @param val the numeric value/*from ww w.  j a va2 s .  c  om*/
 * @param shift how many bits to strip from the right
 * @param bytes will contain the encoded value
 */
public static void longToPrefixCoded(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);
    }
    int nChars = (((63 - shift) * 37) >> 8) + 1; // i/7 is the same as (i*37)>>8 for i in 0..63
    bytes.setLength(nChars + 1); // one extra for the byte that contains the shift info
    bytes.grow(BUF_SIZE_LONG);
    bytes.setByteAt(0, (byte) (SHIFT_START_LONG + shift));
    long sortableBits = val ^ 0x8000000000000000L;
    sortableBits >>>= shift;
    while (nChars > 0) {
        // Store 7 bits per byte for compatibility
        // with UTF-8 encoding of terms
        bytes.setByteAt(nChars--, (byte) (sortableBits & 0x7f));
        sortableBits >>>= 7;
    }
}

From source file:org.apache.solr.legacy.LegacyNumericUtils.java

License:Apache License

/**
 * Returns prefix coded bits after reducing the precision by <code>shift</code> bits.
 * This is method is used by {@link org.apache.solr.legacy.LegacyNumericTokenStream}.
 * After encoding, {@code bytes.offset} will always be 0.
 * @param val the numeric value// w  w w .  jav a  2  s  .  co m
 * @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) {
    // ensure shift is 0..31
    if ((shift & ~0x1f) != 0) {
        throw new IllegalArgumentException("Illegal shift value, must be 0..31; got shift=" + shift);
    }
    int nChars = (((31 - shift) * 37) >> 8) + 1; // i/7 is the same as (i*37)>>8 for i in 0..63
    bytes.setLength(nChars + 1); // one extra for the byte that contains the shift info
    bytes.grow(LegacyNumericUtils.BUF_SIZE_LONG); // use the max
    bytes.setByteAt(0, (byte) (SHIFT_START_INT + shift));
    int sortableBits = val ^ 0x80000000;
    sortableBits >>>= shift;
    while (nChars > 0) {
        // Store 7 bits per byte for compatibility
        // with UTF-8 encoding of terms
        bytes.setByteAt(nChars--, (byte) (sortableBits & 0x7f));
        sortableBits >>>= 7;
    }
}

From source file:org.apache.solr.schema.DatePointField.java

License:Apache License

@Override
public void readableToIndexed(CharSequence val, BytesRefBuilder result) {
    Date date = (Date) toNativeType(val.toString());
    result.grow(Long.BYTES);/* w ww.  j a  v  a  2  s  .co m*/
    result.setLength(Long.BYTES);
    LongPoint.encodeDimension(date.getTime(), result.bytes(), 0);
}

From source file:org.apache.solr.schema.DoublePointField.java

License:Apache License

@Override
public void readableToIndexed(CharSequence val, BytesRefBuilder result) {
    result.grow(Double.BYTES);//from ww w  .j  a v  a  2 s . com
    result.setLength(Double.BYTES);
    DoublePoint.encodeDimension(Double.parseDouble(val.toString()), result.bytes(), 0);
}

From source file:org.apache.solr.schema.EnumFieldType.java

License:Apache License

@Override
public void readableToIndexed(CharSequence val, BytesRefBuilder result) {
    final String s = val.toString();
    if (s == null)
        return;// ww  w . j ava  2 s. c  om

    result.grow(Integer.BYTES);
    result.setLength(Integer.BYTES);
    final Integer intValue = enumMapping.stringValueToIntValue(s);
    NumericUtils.intToSortableBytes(intValue, result.bytes(), 0);
}

From source file:org.apache.solr.schema.EnumFieldType.java

License:Apache License

@Override
public String storedToIndexed(IndexableField f) {
    final Number val = f.numericValue();
    if (val == null)
        return null;
    final BytesRefBuilder bytes = new BytesRefBuilder();
    bytes.grow(Integer.BYTES);/*from   w  w w. j  a v a 2s.c  o m*/
    bytes.setLength(Integer.BYTES);
    NumericUtils.intToSortableBytes(val.intValue(), bytes.bytes(), 0);
    return bytes.get().utf8ToString();
}

From source file:org.apache.solr.schema.FloatPointField.java

License:Apache License

@Override
public void readableToIndexed(CharSequence val, BytesRefBuilder result) {
    result.grow(Float.BYTES);//  w w w . j a va2 s .co  m
    result.setLength(Float.BYTES);
    FloatPoint.encodeDimension(Float.parseFloat(val.toString()), result.bytes(), 0);
}

From source file:org.apache.solr.schema.IntPointField.java

License:Apache License

@Override
public void readableToIndexed(CharSequence val, BytesRefBuilder result) {
    result.grow(Integer.BYTES);/*from   ww  w.  j  av a2s  .c om*/
    result.setLength(Integer.BYTES);
    IntPoint.encodeDimension(Integer.parseInt(val.toString()), result.bytes(), 0);
}

From source file:org.apache.solr.schema.LongPointField.java

License:Apache License

@Override
public void readableToIndexed(CharSequence val, BytesRefBuilder result) {
    result.grow(Long.BYTES);//w  w w  . j av a 2s.  c  om
    result.setLength(Long.BYTES);
    LongPoint.encodeDimension(Long.parseLong(val.toString()), result.bytes(), 0);
}