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.HashMap;
022import java.util.Map;
023
024/**
025 * Settings defines stream's partitions and computation's concurrency.
026 *
027 * @since 9.3
028 */
029public class Settings {
030    protected final int defaultConcurrency;
031
032    protected final int defaultPartitions;
033
034    protected final Map<String, Integer> concurrencies = new HashMap<>();
035
036    protected final Map<String, Integer> partitions = new HashMap<>();
037
038    /**
039     * Default concurrency and partition to use if not specified explicitly
040     */
041    public Settings(int defaultConcurrency, int defaultPartitions) {
042        this.defaultConcurrency = defaultConcurrency;
043        this.defaultPartitions = defaultPartitions;
044    }
045
046    /**
047     * Set the computation thread pool size.
048     */
049    public Settings setConcurrency(String computationName, int concurrency) {
050        concurrencies.put(computationName, concurrency);
051        return this;
052    }
053
054    public int getConcurrency(String computationName) {
055        return concurrencies.getOrDefault(computationName, defaultConcurrency);
056    }
057
058    /**
059     * Set the number of partitions for a stream.
060     */
061    public Settings setPartitions(String streamName, int partitions) {
062        this.partitions.put(streamName, partitions);
063        return this;
064    }
065
066    public int getPartitions(String streamName) {
067        return partitions.getOrDefault(streamName, defaultPartitions);
068    }
069
070}