Пример #1
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());
     }
   }
 }