@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()); } } }
@Override public Date parse(String i, ParsePosition p) { /* delegate to SimpleDateFormat for easy stuff */ Date d = super.parse(i, p); int milliIndex = p.getIndex(); /* worry aboutthe milliseconds ourselves */ if (null != d && -1 == p.getErrorIndex() && milliIndex + 1 < i.length() && '.' == i.charAt(milliIndex)) { p.setIndex(++milliIndex); // NOTE: ++ to chomp '.' Number millis = millisParser.parse(i, p); if (-1 == p.getErrorIndex()) { int endIndex = p.getIndex(); d = new Date( d.getTime() + (long) (millis.doubleValue() * Math.pow(10, (3 - endIndex + milliIndex)))); } } return d; }
@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()); } } }