Example #1
0
 @Override
 public Object toObject(Fieldable f) {
   if (f instanceof NumericField) {
     final Number val = ((NumericField) f).getNumericValue();
     if (val == null) return badFieldString(f);
     return (type == TrieTypes.DATE) ? new Date(val.longValue()) : val;
   } else {
     // the following code is "deprecated" and only to support pre-3.2 indexes using the old
     // BinaryField encoding:
     final byte[] arr = f.getBinaryValue();
     if (arr == null) return badFieldString(f);
     switch (type) {
       case INTEGER:
         return toInt(arr);
       case FLOAT:
         return Float.intBitsToFloat(toInt(arr));
       case LONG:
         return toLong(arr);
       case DOUBLE:
         return Double.longBitsToDouble(toLong(arr));
       case DATE:
         return new Date(toLong(arr));
       default:
         throw new SolrException(
             SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
     }
   }
 }
 private String convertNumber(Number number) {
   if (Integer.class.isInstance(number)) {
     return NumericUtils.intToPrefixCoded(number.intValue());
   } else if (Double.class.isInstance(number)) {
     return NumericUtils.doubleToPrefixCoded(number.doubleValue());
   } else if (Long.class.isInstance(number)) {
     return NumericUtils.longToPrefixCoded(number.longValue());
   } else if (Float.class.isInstance(number)) {
     return NumericUtils.floatToPrefixCoded(number.floatValue());
   } else if (Byte.class.isInstance(number)) {
     return NumericUtils.intToPrefixCoded(number.intValue());
   } else if (Short.class.isInstance(number)) {
     return NumericUtils.intToPrefixCoded(number.intValue());
   } else if (BigDecimal.class.isInstance(number)) {
     return NumericUtils.doubleToPrefixCoded(number.doubleValue());
   } else if (BigInteger.class.isInstance(number)) {
     return NumericUtils.longToPrefixCoded(number.longValue());
   } else {
     throw new IllegalArgumentException("Unsupported numeric type " + number.getClass().getName());
   }
 }
Example #3
0
 @Override
 public String storedToIndexed(Fieldable f) {
   if (f instanceof NumericField) {
     final Number val = ((NumericField) f).getNumericValue();
     if (val == null)
       throw new SolrException(
           SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: " + f.name());
     switch (type) {
       case INTEGER:
         return NumericUtils.intToPrefixCoded(val.intValue());
       case FLOAT:
         return NumericUtils.intToPrefixCoded(NumericUtils.floatToSortableInt(val.floatValue()));
       case LONG: // fallthrough!
       case DATE:
         return NumericUtils.longToPrefixCoded(val.longValue());
       case DOUBLE:
         return NumericUtils.longToPrefixCoded(
             NumericUtils.doubleToSortableLong(val.doubleValue()));
       default:
         throw new SolrException(
             SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
     }
   } else {
     // the following code is "deprecated" and only to support pre-3.2 indexes using the old
     // BinaryField encoding:
     final byte[] arr = f.getBinaryValue();
     if (arr == null)
       throw new SolrException(
           SolrException.ErrorCode.SERVER_ERROR, "Invalid field contents: " + f.name());
     switch (type) {
       case INTEGER:
         return NumericUtils.intToPrefixCoded(toInt(arr));
       case FLOAT:
         {
           // WARNING: Code Duplication! Keep in sync with o.a.l.util.NumericUtils!
           // copied from NumericUtils to not convert to/from float two times
           // code in next 2 lines is identical to: int v =
           // NumericUtils.floatToSortableInt(Float.intBitsToFloat(toInt(arr)));
           int v = toInt(arr);
           if (v < 0) v ^= 0x7fffffff;
           return NumericUtils.intToPrefixCoded(v);
         }
       case LONG: // fallthrough!
       case DATE:
         return NumericUtils.longToPrefixCoded(toLong(arr));
       case DOUBLE:
         {
           // WARNING: Code Duplication! Keep in sync with o.a.l.util.NumericUtils!
           // copied from NumericUtils to not convert to/from double two times
           // code in next 2 lines is identical to: long v =
           // NumericUtils.doubleToSortableLong(Double.longBitsToDouble(toLong(arr)));
           long v = toLong(arr);
           if (v < 0) v ^= 0x7fffffffffffffffL;
           return NumericUtils.longToPrefixCoded(v);
         }
       default:
         throw new SolrException(
             SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field: " + f.name());
     }
   }
 }