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

com.datatorrent.stram.plan.logical.StreamCodecWrapperForPersistance Maven / Gradle / Ivy

There is a newer version: 3.7.0
Show newest version
/**
 * 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.stram.plan.logical;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.datatorrent.api.Partitioner.PartitionKeys;
import com.datatorrent.api.StreamCodec;
import com.datatorrent.netlet.util.Slice;
import com.datatorrent.stram.plan.logical.LogicalPlan.InputPortMeta;

/**
 * 

StreamCodecWrapperForPersistance class.

* * @since 3.2.0 */ public class StreamCodecWrapperForPersistance implements StreamCodec, Serializable { private StreamCodec specifiedStreamCodec; public Map> inputPortToPartitionMap; public Map> codecsToMerge; private boolean operatorPartitioned; public StreamCodecWrapperForPersistance(Map> inputStreamCodecs, StreamCodec specifiedStreamCodec) { this.codecsToMerge = inputStreamCodecs; this.setSpecifiedStreamCodec(specifiedStreamCodec); inputPortToPartitionMap = new HashMap<>(); } @Override public Object fromByteArray(Slice fragment) { return getSpecifiedStreamCodec().fromByteArray(fragment); } @Override public Slice toByteArray(T o) { return getSpecifiedStreamCodec().toByteArray(o); } @Override public int getPartition(T o) { return getSpecifiedStreamCodec().getPartition(o); } public boolean shouldCaptureEvent(T o) { for (Entry> entry : inputPortToPartitionMap.entrySet()) { StreamCodec codec = codecsToMerge.get(entry.getKey()); Collection partitionKeysList = entry.getValue(); for (PartitionKeys keys : partitionKeysList) { if ( keys.partitions != null && keys.partitions.contains(keys.mask & codec.getPartition(o))) { // Then at least one of the partitions is getting this event // So send the event to persist operator return true; } } } return false; } public StreamCodec getSpecifiedStreamCodec() { if (specifiedStreamCodec == null) { specifiedStreamCodec = new DefaultKryoStreamCodec(); } return specifiedStreamCodec; } public void setSpecifiedStreamCodec(StreamCodec specifiedStreamCodec) { this.specifiedStreamCodec = specifiedStreamCodec; } public boolean isOperatorPartitioned() { return operatorPartitioned; } public void setOperatorPartitioned(boolean operatorPartitioned) { this.operatorPartitioned = operatorPartitioned; } }