com.datatorrent.lib.math.CompareExceptMap 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.DefaultOutputPort;
import com.datatorrent.api.annotation.OutputPortFieldAnnotation;
import com.datatorrent.api.annotation.Stateless;
import com.datatorrent.lib.algo.MatchMap;
import com.datatorrent.lib.util.UnifierHashMap;
/**
* Operator compares based on the property "key", "value", and "compare".
*
* The comparison is done by getting double value from the Number.
* Passed tuples are emitted on the output port "compare".
* Failed tuples are emitted on port "except".
* Both output ports are optional, but at least one has to be connected.
* This module is a pass through
*
* Ports:
* data: expects Map<K,V>
* compare: emits HashMap<K,V>
* except: emits HashMap<K,V>
*
* Properties:
* key: The key on which compare is done
* value: The value to compare with
* cmp: The compare function. Supported values are "lte", "lt", "eq", "neq", "gt", "gte". Default is "eq"
*
* Compile time checks
* Key must be non empty
* Value must be able to convert to a "double"
* Compare string, if specified, must be one of "lte", "lt", "eq", "neq", "gt", "gte"
* Specific run time checks:
* Does the incoming HashMap have the key
* Is the value of the key a number
*
* Benchmarks: Blast as many tuples as possible in inline mode
*
* In-Bound Out-bound Comments
* 5 Million K,V pairs/s Each tuple is emitted if emitError is set to true In-bound rate determines performance as every tuple is emitted.
* Immutable tuples were used in the benchmarking. If you use mutable tuples and have lots of keys, the benchmarks may be lower
*
*
* Function Table (K=String, V=Integer); emitError=true; key=a; value=3; cmp=eq):
*
* Tuple Type (api) In-bound (process) Out-bound (emit)
* data(HashMap<K,V>) compare(HashMap<K,V>) except(HashMap<K,V>)
* Begin Window (beginWindow()) N/A N/A N/A
* Data (process()) {a=2,b=20,c=1000} {a=2,b=20,c=1000}
* Data (process()) {a=3,b=40,c=2} {a=3,b=40,c=2}
* Data (process()) {a=10,b=5} {a=10,b=5}
* Data (process()) {d=55,b=12} {d=55,b=12}
* Data (process()) {d=22,a=4} {d=22,a=4}
* Data (process()) {d=4,a=3,g=5,h=44} {d=4,a=3,g=5,h=44}
* End Window (endWindow()) N/A N/A N/A
*
*
*
* @displayName Compare Except Map
* @category Math
* @tags comparison, key value, number, hash map
* @since 0.3.2
*/
@Stateless
public class CompareExceptMap extends MatchMap
{
/**
* Output port that emits a hashmap of matched tuples after comparison.
*/
@OutputPortFieldAnnotation(optional = true)
public final transient DefaultOutputPort> compare = match;
/**
* Output port that emits a hashmap of non matching tuples after comparison.
*/
@OutputPortFieldAnnotation(optional = true)
public final transient DefaultOutputPort> except = new DefaultOutputPort>()
{
@Override
public Unifier> getUnifier()
{
return new UnifierHashMap();
}
};
/**
* Emits if compare port is connected
* @param tuple
*/
@Override
public void tupleMatched(Map tuple)
{
if (compare.isConnected()) {
compare.emit(cloneTuple(tuple));
}
}
/**
* Emits if except port is connected
* @param tuple
*/
@Override
public void tupleNotMatched(Map tuple)
{
if (except.isConnected()) {
except.emit(cloneTuple(tuple));
}
}
}