All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datatorrent.lib.math.RangeKeyVal Maven / Gradle / Ivy

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package com.datatorrent.lib.math;

import java.util.HashMap;
import java.util.Map;

import com.datatorrent.api.DefaultInputPort;
import com.datatorrent.api.DefaultOutputPort;
import com.datatorrent.api.StreamCodec;
import com.datatorrent.lib.util.BaseNumberKeyValueOperator;
import com.datatorrent.lib.util.HighLow;
import com.datatorrent.lib.util.KeyValPair;
import com.datatorrent.lib.util.UnifierKeyValRange;

/**
 *  This operator emits the range for each key at the end of window.
 * 

*
* StateFull : Yes, values are computed over application window.
* Partitions : Yes, high/low values are each key is unified at output port.
*
* Ports:
* data: expects KeyValPair<K,V extends Number>
* range: emits KeyValPair<K,HighLow<V>>
*
* Properties:
* inverse: if set to true the key in the filter will block tuple
* filterBy: List of keys to filter on
*
* @displayName Range Key Value * @category Math * @tags range, number, comparison, key value * @since 0.3.3 */ public class RangeKeyVal extends BaseNumberKeyValueOperator { /** * key/high value map. */ protected HashMap high = new HashMap(); /** * key/low value map. */ protected HashMap low = new HashMap(); /** * Input port that takes a key value pair. */ public final transient DefaultInputPort> data = new DefaultInputPort>() { /** * Process each key and computes new high and low. */ @Override public void process(KeyValPair tuple) { K key = tuple.getKey(); if (!doprocessKey(key) || (tuple.getValue() == null)) { return; } V val = low.get(key); V eval = tuple.getValue(); if ((val == null) || (val.doubleValue() > eval.doubleValue())) { low.put(cloneKey(key), eval); } val = high.get(key); if ((val == null) || (val.doubleValue() < eval.doubleValue())) { high.put(cloneKey(key), eval); } } @Override public StreamCodec> getStreamCodec() { return getKeyValPairStreamCodec(); } }; /** * Range output port to send out the high low range. */ public final transient DefaultOutputPort>> range = new DefaultOutputPort>>() { @Override public Unifier>> getUnifier() { return new UnifierKeyValRange(); } }; /** * Emits range for each key. If no data is received, no emit is done Clears * the internal data before return */ @Override public void endWindow() { for (Map.Entry e : high.entrySet()) { range.emit(new KeyValPair>(e.getKey(), new HighLow(e .getValue(), low.get(e.getKey())))); } clearCache(); } public void clearCache() { high.clear(); low.clear(); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy