public void addValue(T value) { if (value == null) return; byte[] valueBytes = bytesConverter.convertToBytes(value); addValue(valueBytes); }
@Override protected int getIdFromValueBytesWithoutCache(byte[] value, int offset, int len, int roundingFlag) { int seq = lookupSeqNoFromValue(headSize, value, offset, offset + len, roundingFlag); int id = calcIdFromSeqNo(seq); if (id < 0) logger.debug("Not a valid value: " + bytesConvert.convertFromBytes(value, offset, len)); return id; }
@Override protected byte[] getValueBytesFromIdImpl(int id) { if (idToValueByteCache != null) { byte[][] bytes = idToValueByteCache.get(); if (bytes != null) { int seq = calcSeqNoFromId(id); if (bytes[seq] != null) { cacheHitCount++; return bytes[seq]; } byte[] valueBytes = getValueBytesFromIdWithoutCache(id); byte[] bytes1 = bytesConvert.convertBytesValueFromBytes(valueBytes, 0, valueBytes.length); bytes[seq] = bytes1; cacheMissCount++; return bytes1; } } byte[] valueBytes = getValueBytesFromIdWithoutCache(id); return bytesConvert.convertBytesValueFromBytes(valueBytes, 0, valueBytes.length); }
public void addValue(T value) { addValue(bytesConverter.convertToBytes(value)); }
@Override public void dump(PrintStream out) { out.println("TrieDictionaryForest"); out.println("baseId:" + baseId); StringBuilder sb = new StringBuilder(); sb.append("value divide:"); for (ByteArray ba : valueDivide) sb.append(bytesConvert.convertFromBytes(ba.array(), 0, ba.length()) + " "); sb.append("\noffset divide:"); for (Integer offset : accuOffset) sb.append(offset + " "); out.println(sb.toString()); for (int i = 0; i < trees.size(); i++) { out.println("----tree " + i + "--------"); trees.get(i).dump(out); } }
@Override protected byte[] getValueBytesFromIdImpl(int id) { if (idToValueByteCache != null) { byte[][] bytes = idToValueByteCache.get(); if (bytes != null) { int seq = calcSeqNoFromId(id); if (bytes[seq] != null) { cacheHitCount++; return bytes[seq]; } byte[] valueBytes = getValueBytesFromIdWithoutCache(id); byte[] bytes1 = bytesConvert.convertBytesValueFromBytes(valueBytes, 0, valueBytes.length); bytes[seq] = bytes1; cacheMissCount++; return bytes1; } } byte[] valueBytes = getValueBytesFromIdWithoutCache(id); return bytesConvert.convertBytesValueFromBytes(valueBytes, 0, valueBytes.length); }
@Override protected final int getIdFromValueImpl(T value, int roundingFlag) { if (this.valueToIdCache != null && roundingFlag == 0) { Map cache = valueToIdCache.get(); // SoftReference to skip cache gracefully when short of memory if (cache != null) { Integer id; id = (Integer) cache.get(value); if (id != null) return id.intValue(); byte[] valueBytes = bytesConvert.convertToBytes(value); id = getIdFromValueBytesWithoutCache(valueBytes, 0, valueBytes.length, roundingFlag); cache.put(value, id); return id; } } byte[] valueBytes = bytesConvert.convertToBytes(value); return getIdFromValueBytesWithoutCache(valueBytes, 0, valueBytes.length, roundingFlag); }
@Override protected final T getValueFromIdImpl(int id) { if (this.idToValueCache != null) { Object[] cache = idToValueCache.get(); if (cache != null) { int seq = calcSeqNoFromId(id); if (cache[seq] != null) return (T) cache[seq]; byte[] valueBytes = getValueBytesFromIdWithoutCache(id); T value = bytesConvert.convertFromBytes(valueBytes, 0, valueBytes.length); cache[seq] = value; return value; } } byte[] valueBytes = getValueBytesFromIdWithoutCache(id); return bytesConvert.convertFromBytes(valueBytes, 0, valueBytes.length); }
@Override public int compare(T o1, T o2) { //return BytesUtil.safeCompareBytes(converter.convertToBytes(o1),converter.convertToBytes(o2)); byte[] b1 = converter.convertToBytes(o1); byte[] b2 = converter.convertToBytes(o2); ByteArray ba1 = new ByteArray(b1, 0, b1.length); ByteArray ba2 = new ByteArray(b2, 0, b2.length); return ba1.compareTo(ba2); } }
T curNodeValue = bytesConvert.convertFromBytes(returnValue, 0, o); result.add(curNodeValue);
private void initMaxValueForEachTrie() { //init max value this.maxValue = new ArrayList<>(); if (this.trees == null || trees.isEmpty()) { return; } for (int i = 0; i < trees.size(); i++) { T curTreeMax = trees.get(i).getValueFromId(trees.get(i).getMaxId()); byte[] b1 = bytesConvert.convertToBytes(curTreeMax); ByteArray ba1 = new ByteArray(b1, 0, b1.length); this.maxValue.add(ba1); } }
@Override protected int getIdFromValueBytesImpl(byte[] value, int offset, int len, int roundingFlag) { int seq = lookupSeqNoFromValue(headSize, value, offset, offset + len, roundingFlag); int id = calcIdFromSeqNo(seq); if (id < 0) throw new IllegalArgumentException("Not a valid value: " + bytesConvert.convertFromBytes(value, offset, len)); return id; }
@SuppressWarnings("unchecked") public void addValue(String value) throws IOException { byte[] valueBytes = bytesConverter.convertToBytes(value);
@Override final protected T getValueFromIdImpl(int id) { if (enableCache) { Object[] cache = idToValueCache.get(); // SoftReference to skip // cache gracefully when // short of memory if (cache != null) { int seq = calcSeqNoFromId(id); if (seq < 0 || seq >= nValues) throw new IllegalArgumentException("Not a valid ID: " + id); if (cache[seq] != null) return (T) cache[seq]; byte[] value = new byte[getSizeOfValue()]; int length = getValueBytesFromId(id, value, 0); T result = bytesConvert.convertFromBytes(value, 0, length); cache[seq] = result; return result; } } byte[] value = new byte[getSizeOfValue()]; int length = getValueBytesFromId(id, value, 0); return bytesConvert.convertFromBytes(value, 0, length); }
strArray[id] = value; array[id] = converter.convertToBytes(value);
@Override protected int getIdFromValueBytesImpl(byte[] value, int offset, int len, int roundingFlag) { int seq = lookupSeqNoFromValue(headSize, value, offset, offset + len, roundingFlag); int id = calcIdFromSeqNo(seq); if (id < 0) throw new IllegalArgumentException("Not a valid value: '" + bytesConvert.convertFromBytes(value, offset, len) + "'"); return id; }
BytesConverter converter = new StringBytesConverter(); for (String str : strList) { byte[] bytes = converter.convertToBytes(str); int id = dict.getIdFromValueBytesWithoutCache(bytes, 0, bytes.length, 0); checkMap.put(id, str); byte[] bytes = converter.convertToBytes(str); int id = r.getIdFromValueBytesWithoutCache(bytes, 0, bytes.length, 0); assertNotEquals(String.format(Locale.ROOT, "Value %s not exist", str), -1, id);
@Override protected int getIdFromValueBytesWithoutCache(byte[] value, int offset, int len, int roundingFlag) { int seq = lookupSeqNoFromValue(headSize, value, offset, offset + len, roundingFlag); int id = calcIdFromSeqNo(seq); if (id < 0) logger.debug("Not a valid value: " + bytesConvert.convertFromBytes(value, offset, len)); return id; }
for (; checkIndex < firstAppend; checkIndex++) { String str = strList.get(checkIndex); byte[] bytes = converter.convertToBytes(str); int id = dict.getIdFromValueBytesWithoutCache(bytes, 0, bytes.length, 0); assertNotEquals(String.format(Locale.ROOT, "Value %s not exist", str), -1, id); for (; checkIndex < secondAppend; checkIndex++) { String str = strList.get(checkIndex); byte[] bytes = converter.convertToBytes(str); int id = dict.getIdFromValueBytesWithoutCache(bytes, 0, bytes.length, 0); assertNotEquals(String.format(Locale.ROOT, "Value %s not exist", str), -1, id); for (; checkIndex < strList.size(); checkIndex++) { String str = strList.get(checkIndex); byte[] bytes = converter.convertToBytes(str); int id = dict.getIdFromValueBytesWithoutCache(bytes, 0, bytes.length, 0); assertNotEquals(String.format(Locale.ROOT, "Value %s not exist", str), -1, id); byte[] bytes = converter.convertToBytes(s); int id = dict.getIdFromValueBytesWithoutCache(bytes, 0, bytes.length, 0); assertEquals(-1, id); byte[] bytes = converter.convertToBytes(str); int id = dict.getIdFromValueBytesWithoutCache(bytes, 0, bytes.length, 0); assertNotEquals(String.format(Locale.ROOT, "Value %s not exist", str), -1, id);
@Override public void dump(PrintStream out) { out.println("TrieDictionaryForest"); out.println("baseId:" + baseId); StringBuilder sb = new StringBuilder(); sb.append("value divide:"); for (ByteArray ba : valueDivide) sb.append(bytesConvert.convertFromBytes(ba.array(), 0, ba.length()) + " "); sb.append("\noffset divide:"); for (Integer offset : accuOffset) sb.append(offset + " "); out.println(sb.toString()); for (int i = 0; i < trees.size(); i++) { out.println("----tree " + i + "--------"); trees.get(i).dump(out); } }