001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.computation;
020
021import java.util.stream.Collectors;
022import java.util.stream.IntStream;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027/**
028 * An abstract {@link Computation} that manages the metadata init.
029 * <p>
030 * By convention the inputs streams are named internally: i1, i2 ...
031 * <p>
032 * and the output streams are named: o1, o2 ...
033 *
034 * @since 9.3
035 */
036public abstract class AbstractComputation implements Computation {
037    private static final Log log = LogFactory.getLog(AbstractComputation.class);
038
039    protected final ComputationMetadata metadata;
040
041    public static final String INPUT_1 = "i1";
042
043    public static final String INPUT_2 = "i2";
044
045    public static final String INPUT_3 = "i3";
046
047    public static final String OUTPUT_1 = "o1";
048
049    public static final String OUTPUT_2 = "o2";
050
051    public static final String OUTPUT_3 = "o3";
052
053    public static final String OUTPUT_4 = "o4";
054
055    // @since 11.1 can be used as input for single producer pattern
056    public static final String INPUT_NULL = "input/null";
057
058    /**
059     * Creates a computation with the requested number of input and output streams.
060     *
061     * @since 10.3
062     */
063    public AbstractComputation(String name, int nbInputStreams, int nbOutputStreams) {
064        this.metadata = new ComputationMetadata(name,
065                IntStream.range(1, nbInputStreams + 1).boxed().map(i -> "i" + i).collect(Collectors.toSet()),
066                IntStream.range(1, nbOutputStreams + 1).boxed().map(i -> "o" + i).collect(Collectors.toSet()));
067    }
068
069    @Override
070    public void init(ComputationContext context) {
071
072    }
073
074    @Override
075    public void processTimer(ComputationContext context, String key, long timestamp) {
076
077    }
078
079    @Override
080    public ComputationMetadata metadata() {
081        return metadata;
082    }
083
084    @Override
085    public void processRetry(ComputationContext context, Throwable failure) {
086        log.warn(String.format("Computation: %s fails last record: %s, retrying ...", metadata.name(),
087                context.getLastOffset()), failure);
088    }
089
090    @Override
091    public void processFailure(ComputationContext context, Throwable failure) {
092        log.error(String.format("Computation: %s fails last record: %s, after retries.", metadata.name(),
093                context.getLastOffset()), failure);
094    }
095}