@Override public Entry<String, Double> next() { Entry<String, ValueObject> input = iterator.next(); return new SimpleEntry<>(input.getKey(), input.getValue().asDouble().getDouble()); }
@Override public Double remove(Object key) { String stringKey = (String) key; ValueObject val = data.remove(stringKey); if (val == null) { return null; } else { return val.asDouble().getDouble(); } }
@Override public Double remove(int index) { ValueObject prev = data.remove(index); if (prev == null) return null; return prev.asDouble().getDouble(); }
@Override public Double get(int index) { ValueObject val = data.get(index); if (val == null) return null; return val.asDouble().getDouble(); }
@Override public ValueDouble avg(int count) { return ValueFactory.create(asDouble().getDouble() / (double) count); }
@Override public Double get(Object key) { String stringKey = (String) key; ValueObject val = data.get(stringKey); if (val == null) { return null; } else { return val.asDouble().getDouble(); } }
/** * @param s1 * @param s2 * @return */ private int doubleCompare(ValueObject s1, ValueObject s2) { if (s1 == s2) { return 0; } if (s1 == null) { return 1; } if (s2 == null) { return -1; } return Double.compare(ValueUtil.asNumberOrParseDouble(s1).asDouble().getDouble(), ValueUtil.asNumberOrParseDouble(s2).asDouble().getDouble()); }
/** * @param s1 * @param s2 * @return */ private static int doubleCompare(ValueObject s1, ValueObject s2) { if (s1 == s2) { return 0; } if (s1 == null) { return 1; } if (s2 == null) { return -1; } return Double.compare(ValueUtil.asNumberOrParseDouble(s1).asDouble().getDouble(), ValueUtil.asNumberOrParseDouble(s2).asDouble().getDouble()); }
@Override public Double put(String key, Double value) { ValueObject val = data.put(key, ValueFactory.create(value)); if (val == null) { return null; } else { return val.asDouble().getDouble(); } }
@Override public Double set(int index, Double element) { ValueObject prev = data.set(index, ValueFactory.create(element)); if (prev == null) return null; return prev.asDouble().getDouble(); }
@Override public void setValues(ValueMap valueMapEntries) { byte[] b = valueMapEntries.get("b").asBytes().asNative(); this.quantile = valueMapEntries.get("q").asDouble().getDouble(); this.op = OP.valueOf(valueMapEntries.get("o").asString().toString()); heldObject = TDigest.fromBytes(ByteBuffer.wrap(b)); }
private Object unbox(ValueObject val) { switch (val.getObjectType()) { case INT: return val.asLong().getLong(); case FLOAT: return val.asDouble().getDouble(); case STRING: return val.asString().asNative(); case ARRAY: List<Object> retList = new LinkedList<>(); for (ValueObject element : val.asArray()) { retList.add(unbox(element)); } return retList; case MAP: Map<String, Object> retMap = new HashMap<>(); ValueMap valAsMap = val.asMap(); for (ValueMapEntry entry : valAsMap) { retMap.put(entry.getKey(), unbox(entry.getValue())); } return retMap; } throw new IllegalArgumentException("Unsupported bundle field type: " + val.getObjectType()); }
/** * updates the TDigest with value from the key field. Expectation is that the value * is a number. */ @Override public boolean updateChildData(DataTreeNodeUpdater state, DataTreeNode childNode, Config conf) { Bundle p = state.getBundle(); if (valueAccess == null) { valueAccess = p.getFormat().getField(conf.key); } Numeric o = ValueUtil.asNumberOrParseDouble(p.getValue(valueAccess)); if (o != null) { filter.add(o.asDouble().getDouble()); return true; } return false; }
@Override public Bundle rowOp(Bundle row) { if (binder == null) { binder = getSourceColumnBinder(row); rowFactory = row.createBundle(); } Numeric value = ValueUtil.asNumberOrParse(binder.getColumn(row, column)); long increment; if (frequency >= 0) { Numeric incNumeric = ValueUtil.asNumberOrParseLong(binder.getColumn(row, frequency), 10); increment = (incNumeric != null) ? incNumeric.asLong().asNative() : 1; } else { increment = 1; } if (mode == Mode.FLOAT) { float target = (float) value.asDouble().getDouble(); int position = Arrays.binarySearch(keys, target); if (position < 0) position = ~position - 1; counts[position] += increment; } else { int target = (int) value.asLong().getLong(); int position = Arrays.binarySearch(keys, target); if (position < 0) position = ~position - 1; counts[position] += increment; } return null; }
@Override public ValueObject filterValue(ValueObject value) { if (value == null) { return value; } try { Boolean res = false; if (rh != null) { long val = ValueUtil.asNumberOrParseLong(value, 10).asLong().getLong(); res = evalExpression(iop, val, rh); } else if (rhd != null) { double val = ValueUtil.asNumberOrParseDouble(value).asDouble().getDouble(); res = evalExpressionD(iop, val, rhd); } if (res) { return value; } else { return null; } } catch (Exception ex) { return value; } }
@Test public void testGetValue() { DataReservoir reservoir = new DataReservoir(); reservoir.updateReservoir(1, 4, 4); reservoir.updateReservoir(2, 4, 12); reservoir.updateReservoir(3, 4, 4); reservoir.updateReservoir(4, 4, 100); ValueArray result = reservoir.getValue("epoch||4~sigma||2.0~obs||3").asArray(); assertEquals(5, result.size()); assertEquals(86, DoubleMath.roundToLong(result.get(0).asDouble().getDouble(), RoundingMode.HALF_UP)); assertEquals(100, result.get(1).asLong().getLong()); assertEquals(7, DoubleMath.roundToLong(result.get(2).asDouble().getDouble(), RoundingMode.HALF_UP)); assertEquals(4, DoubleMath.roundToLong(result.get(3).asDouble().getDouble(), RoundingMode.HALF_UP)); assertEquals(14, result.get(4).asLong().getLong()); // test mode "get" assertEquals(0, reservoir.getValue("mode||get~epoch||0").asLong().getLong()); assertEquals(4, reservoir.getValue("mode||get~epoch||1").asLong().getLong()); assertEquals(12, reservoir.getValue("mode||get~epoch||2").asLong().getLong()); assertEquals(4, reservoir.getValue("mode||get~epoch||3").asLong().getLong()); assertEquals(100, reservoir.getValue("mode||get~epoch||4").asLong().getLong()); assertEquals(0, reservoir.getValue("mode||get~epoch||5").asLong().getLong()); }
@Test public void testGetSerialization() { DataReservoir reservoir = new DataReservoir(); reservoir.updateReservoir(1, 4, 4); reservoir.updateReservoir(2, 4, 12); reservoir.updateReservoir(3, 4, 4); reservoir.updateReservoir(4, 4, 100); DataReservoir.DataReservoirValue original = (DataReservoir.DataReservoirValue) reservoir.getValue("epoch||4~sigma||2.0~obs||3"); DataReservoir.DataReservoirValue translated = new DataReservoir.DataReservoirValue(); translated.setValues(original.asMap()); ValueArray result = translated.asArray(); assertEquals(5, result.size()); assertEquals(86, DoubleMath.roundToLong(result.get(0).asDouble().getDouble(), RoundingMode.HALF_UP)); assertEquals(100, result.get(1).asLong().getLong()); assertEquals(7, DoubleMath.roundToLong(result.get(2).asDouble().getDouble(), RoundingMode.HALF_UP)); assertEquals(4, DoubleMath.roundToLong(result.get(3).asDouble().getDouble(), RoundingMode.HALF_UP)); assertEquals(14, result.get(4).asLong().getLong()); // test mode "get" assertEquals(0, reservoir.getValue("mode||get~epoch||0").asLong().getLong()); assertEquals(4, reservoir.getValue("mode||get~epoch||1").asLong().getLong()); assertEquals(12, reservoir.getValue("mode||get~epoch||2").asLong().getLong()); assertEquals(4, reservoir.getValue("mode||get~epoch||3").asLong().getLong()); assertEquals(100, reservoir.getValue("mode||get~epoch||4").asLong().getLong()); assertEquals(0, reservoir.getValue("mode||get~epoch||5").asLong().getLong()); }