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

com.datatorrent.lib.counters.BasicCounters 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.counters;

import java.io.IOException;
import java.io.Serializable;
import java.util.Collection;
import java.util.Map;

import javax.annotation.Nonnull;

import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider;
import org.codehaus.jackson.map.annotate.JsonSerialize;

import org.apache.commons.lang.mutable.Mutable;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;

import com.datatorrent.api.Context;
import com.datatorrent.common.util.NumberAggregate;

/**
 * Implementation of basic number counters.
 *
 * @param  type of counter
 * @since 1.0.2
 */
@JsonSerialize(using = BasicCounters.Serializer.class)
public class BasicCounters implements Serializable
{
  private final Map, T> cache;
  private final Class counterType;

  @SuppressWarnings("unused")
  private BasicCounters()
  {
    //for kryo
    cache = null;
    counterType = null;
  }

  /**
   * @param counterType type of counter
   */
  public BasicCounters(@Nonnull Class counterType)
  {
    cache = Maps.newIdentityHashMap();
    this.counterType = counterType;
  }

  /**
   * Returns the counter associated with the key. Creates it if it doesn't exist.
   */
  @SuppressWarnings("unchecked")
  public synchronized T findCounter(Enum counterKey) throws IllegalAccessException, InstantiationException
  {
    T counter = cache.get(counterKey);
    if (counter == null) {
      counter = counterType.newInstance();
      cache.put(counterKey, counter);
    }
    return counter;
  }

  /**
   * Returns the counter if it exists; null otherwise
   *
   * @param counterKey
   * @return counter corresponding to the counter key.
   */
  public synchronized T getCounter(Enum counterKey)
  {
    return cache.get(counterKey);
  }

  /**
   * Sets the value of a counter.
   *
   * @param counterKey counter key.
   * @param value      new value.
   */
  public synchronized void setCounter(Enum counterKey, T value)
  {
    cache.put(counterKey, value);
  }

  /**
   * Returns an immutable copy of all the counters.
   *
   * @return an immutable copy of the counters.
   */
  public synchronized ImmutableMap, T> getCopy()
  {
    return ImmutableMap.copyOf(cache);
  }

  public static class Serializer extends JsonSerializer> implements Serializable
  {

    @Override
    public void serialize(BasicCounters value, JsonGenerator jgen, SerializerProvider provider)
      throws IOException
    {
      jgen.writeObject(value.getCopy());
    }

    private static final long serialVersionUID = 201406230131L;

  }

  /**
   * Aggregates the {@link BasicCounters} of physical partitions into {@link NumberAggregate.LongAggregate}.
   *
   * @param  type of counter
   */
  public static class LongAggregator implements Context.CountersAggregator, Serializable
  {
    @Override
    public Object aggregate(Collection objects)
    {
      Map aggregates = Maps.newHashMap();
      for (Object counter : objects) {
        if (counter instanceof BasicCounters) {
          @SuppressWarnings("unchecked")
          BasicCounters physical = (BasicCounters)counter;
          ImmutableMap, T> copy = physical.getCopy();

          for (Map.Entry, T> entry : copy.entrySet()) {

            NumberAggregate.LongAggregate aggregate = aggregates.get(entry.getKey().name());

            if (aggregate == null) {
              aggregate = new NumberAggregate.LongAggregate();

              aggregates.put(entry.getKey().name(), aggregate);
            }
            aggregate.addNumber(entry.getValue().longValue());
          }
        }
      }
      return ImmutableMap.copyOf(aggregates);
    }

    private static final long serialVersionUID = 201406222203L;
  }

  /**
   * Aggregates the {@link BasicCounters} of physical partitions into {@link NumberAggregate.DoubleAggregate}.
   *
   * @param  type of counter
   */
  public static class DoubleAggregator implements Context.CountersAggregator, Serializable
  {
    @Override
    public Object aggregate(Collection objects)
    {
      Map aggregates = Maps.newHashMap();
      for (Object counter : objects) {
        if (counter instanceof BasicCounters) {
          @SuppressWarnings("unchecked")
          BasicCounters physical = (BasicCounters)counter;
          ImmutableMap, T> copy = physical.getCopy();

          for (Map.Entry, T> entry : copy.entrySet()) {

            NumberAggregate.DoubleAggregate aggregate = aggregates.get(entry.getKey().name());

            if (aggregate == null) {
              aggregate = new NumberAggregate.DoubleAggregate();

              aggregates.put(entry.getKey().name(), aggregate);
            }
            aggregate.addNumber(entry.getValue().doubleValue());
          }
        }
      }
      return ImmutableMap.copyOf(aggregates);
    }

    private static final long serialVersionUID = 201407011713L;
  }

  private static final long serialVersionUID = 201406230033L;
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy