Class NumericEncoder


  • public class NumericEncoder
    extends java.lang.Object
    Support to encode numeric types in the lucene index. To support range queries in the lucene index numeric types need to be indexed specially. This has been addressed for int and long types for lucene and limited support (via scaling) for float and double. The implementation splits an int, long, float or double into the sign bit, optional exponent and mantissa either from the int or long format or its IEEE 754 byte representation. To index content so small negative numbers are indexed correctly and are after big negative numbers in range queries. The algorithm finds the sign, if the number is negative, then the mantissa and exponent are XORed against the appropriate masks. This reverses the order. As negative numbers appear first in the list their sign bit is 0 and positive numbers are 1.
    Author:
    Andy Hind
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int decodeInt​(java.lang.String hex)  
      static long decodeLong​(java.lang.String hex)
      Secode a long
      static java.lang.String encode​(double doubleToEncode)
      Encode a double into a string that orders correctly according to string comparison.
      static java.lang.String encode​(float floatToEncode)
      Encode a float into a string that orders correctly according to string comparison.
      static java.lang.String encode​(int intToEncode)
      Encode an integer into a string that orders correctly using string comparison Integer.MIN_VALUE encodes as 00000000 and MAX_VALUE as ffffffff.
      static java.lang.String encode​(long longToEncode)
      Encode a long into a string that orders correctly using string comparison Long.MIN_VALUE encodes as 0000000000000000 and MAX_VALUE as ffffffffffffffff.
      static java.lang.String encode​(java.lang.Long longToEncode)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • encode

        public static java.lang.String encode​(int intToEncode)
        Encode an integer into a string that orders correctly using string comparison Integer.MIN_VALUE encodes as 00000000 and MAX_VALUE as ffffffff.
        Parameters:
        intToEncode - int
        Returns:
        the encoded string
      • encode

        public static java.lang.String encode​(long longToEncode)
        Encode a long into a string that orders correctly using string comparison Long.MIN_VALUE encodes as 0000000000000000 and MAX_VALUE as ffffffffffffffff.
        Parameters:
        longToEncode - long
        Returns:
        - the encoded string
      • encode

        public static java.lang.String encode​(java.lang.Long longToEncode)
      • decodeLong

        public static long decodeLong​(java.lang.String hex)
        Secode a long
        Parameters:
        hex - String
        Returns:
        - the decoded string
      • decodeInt

        public static int decodeInt​(java.lang.String hex)
      • encode

        public static java.lang.String encode​(float floatToEncode)
        Encode a float into a string that orders correctly according to string comparison. Note that there is no negative NaN but there are codings that imply this. So NaN and -Infinity may not compare as expected.
        Parameters:
        floatToEncode - float
        Returns:
        - the encoded string
      • encode

        public static java.lang.String encode​(double doubleToEncode)
        Encode a double into a string that orders correctly according to string comparison. Note that there is no negative NaN but there are codings that imply this. So NaN and -Infinity may not compare as expected.
        Parameters:
        doubleToEncode - double
        Returns:
        the encoded string