/** * Returns the ID integer of given value. In case of not found * <p> * - if roundingFlag=0, throw IllegalArgumentException; <br> * - if roundingFlag<0, the closest smaller ID integer if exist; <br> * - if roundingFlag>0, the closest bigger ID integer if exist. <br> * <p> * The implementation often has cache, thus faster than the byte[] version getIdFromValueBytes() * * @throws IllegalArgumentException * if value is not found in dictionary and rounding is off; * or if rounding cannot find a smaller or bigger ID */ final public int getIdFromValue(T value, int roundingFlag) throws IllegalArgumentException { if (isNullObjectForm(value)) return nullId(); int id = getIdFromValueImpl(value, roundingFlag); if (id == -1) { throw new IllegalArgumentException("Value : " + value + " not exists"); } return id; }
@Override public List<ByteArray> reEncodeDictionary(List<ByteArray> value, MeasureDesc measureDesc, Map<TblColRef, Dictionary<String>> oldDicts, Map<TblColRef, Dictionary<String>> newDicts) { TblColRef colRef = getRawColumn(measureDesc.getFunction()); Dictionary<String> sourceDict = oldDicts.get(colRef); Dictionary<String> mergedDict = newDicts.get(colRef); int valueSize = value.size(); byte[] newIdBuf = new byte[valueSize * mergedDict.getSizeOfId()]; int bufOffset = 0; for (ByteArray c : value) { int oldId = BytesUtil.readUnsigned(c.array(), c.offset(), c.length()); int newId; String v = sourceDict.getValueFromId(oldId); if (v == null) { newId = mergedDict.nullId(); } else { newId = mergedDict.getIdFromValue(v); } BytesUtil.writeUnsigned(newId, newIdBuf, bufOffset, mergedDict.getSizeOfId()); c.reset(newIdBuf, bufOffset, mergedDict.getSizeOfId()); bufOffset += mergedDict.getSizeOfId(); } return value; } };
@Ignore @SuppressWarnings("unchecked") @Test public void testEmptyInput() throws IOException { String[] ints = new String[] { "", "0", "5", "100", "13" }; // check "" is treated as NULL, not a code of dictionary Dictionary<?> dict = DictionaryGenerator.buildDictionary(DataType.getType("integer"), new IterableDictionaryValueEnumerator(ints)); assertEquals(4, dict.getSize()); final int id = ((NumberDictionary<String>) dict).getIdFromValue(""); assertEquals(id, dict.nullId()); }
/** * Returns the ID integer of given value. In case of not found * <p> * - if roundingFlag=0, throw IllegalArgumentException; <br> * - if roundingFlag<0, the closest smaller ID integer if exist; <br> * - if roundingFlag>0, the closest bigger ID integer if exist. <br> * <p> * The implementation often has cache, thus faster than the byte[] version getIdFromValueBytes() * * @throws IllegalArgumentException * if value is not found in dictionary and rounding is off; * or if rounding cannot find a smaller or bigger ID */ final public int getIdFromValue(T value, int roundingFlag) throws IllegalArgumentException { if (isNullObjectForm(value)) return nullId(); int id = getIdFromValueImpl(value, roundingFlag); if (id == -1) { throw new IllegalArgumentException("Value : " + value + " not exists"); } return id; }
/** * A lower level API, return ID integer from raw value bytes. In case of not found * <p> * - if roundingFlag=0, throw IllegalArgumentException; <br> * - if roundingFlag<0, the closest smaller ID integer if exist; <br> * - if roundingFlag>0, the closest bigger ID integer if exist. <br> * <p> * Bypassing the cache layer, this could be significantly slower than getIdFromValue(T value). * * @throws IllegalArgumentException * if value is not found in dictionary and rounding is off or failed */ final public int getIdFromValueBytes(byte[] value, int offset, int len, int roundingFlag) { if (isNullByteForm(value, offset, len)) return nullId(); else return getIdFromValueBytesImpl(value, offset, len, roundingFlag); }
/** * Returns the ID integer of given value. In case of not found * <p> * - if roundingFlag=0, throw IllegalArgumentException; <br> * - if roundingFlag<0, the closest smaller ID integer if exist; <br> * - if roundingFlag>0, the closest bigger ID integer if exist. <br> * <p> * The implementation often has cache, thus faster than the byte[] version getIdFromValueBytes() * * @throws IllegalArgumentException * if value is not found in dictionary and rounding is off or * failed */ final public int getIdFromValue(T value, int roundingFlag) throws IllegalArgumentException { if (isNullObjectForm(value)) return nullId(); else return getIdFromValueImpl(value, roundingFlag); }
idInMergedDict = mergedDict.nullId(); } else { idInMergedDict = mergedDict.getIdFromValueBytes(newKeyBuf, bufOffset, size);
@Override public List<ByteArray> reEncodeDictionary(List<ByteArray> value, MeasureDesc measureDesc, Map<TblColRef, Dictionary<String>> oldDicts, Map<TblColRef, Dictionary<String>> newDicts) { TblColRef colRef = getRawColumn(measureDesc.getFunction()); Dictionary<String> sourceDict = oldDicts.get(colRef); Dictionary<String> mergedDict = newDicts.get(colRef); int valueSize = value.size(); byte[] newIdBuf = new byte[valueSize * mergedDict.getSizeOfId()]; int bufOffset = 0; for (ByteArray c : value) { int oldId = BytesUtil.readUnsigned(c.array(), c.offset(), c.length()); int newId; String v = sourceDict.getValueFromId(oldId); if (v == null) { newId = mergedDict.nullId(); } else { newId = mergedDict.getIdFromValue(v); } BytesUtil.writeUnsigned(newId, newIdBuf, bufOffset, mergedDict.getSizeOfId()); c.reset(newIdBuf, bufOffset, mergedDict.getSizeOfId()); bufOffset += mergedDict.getSizeOfId(); } return value; } };