    001    /*
    002     * Copyright (c) 2007-2014 Concurrent, Inc. All Rights Reserved.
    003     *
    004     * Project and contact information: http://www.cascading.org/
    005     *
    006     * This file is part of the Cascading project.
    007     *
    008     * Licensed under the Apache License, Version 2.0 (the "License");
    009     * you may not use this file except in compliance with the License.
    010     * You may obtain a copy of the License at
    011     *
    012     *     http://www.apache.org/licenses/LICENSE-2.0
    013     *
    014     * Unless required by applicable law or agreed to in writing, software
    015     * distributed under the License is distributed on an "AS IS" BASIS,
    016     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    017     * See the License for the specific language governing permissions and
    018     * limitations under the License.
    019     */
    020
    021    package cascading.stats.local;
    022
    023    import java.util.Collection;
    024    import java.util.Collections;
    025    import java.util.HashMap;
    026    import java.util.HashSet;
    027    import java.util.Map;
    028    import java.util.Properties;
    029    import java.util.Set;
    030
    031    import cascading.flow.FlowStep;
    032    import cascading.management.state.ClientState;
    033    import cascading.stats.FlowStepStats;
    034
    035    /**
    036     *
    037     */
    038    public class LocalStepStats extends FlowStepStats
    039      {
    040      final Map<String, Map<String, Long>> counters = new HashMap<String, Map<String, Long>>();
    041
    042      /** Constructor CascadingStats creates a new CascadingStats instance. */
    043      public LocalStepStats( FlowStep<Properties> flowStep, ClientState clientState )
    044        {
    045        super( flowStep, clientState );
    046        }
    047
    048      @Override
    049      public void recordChildStats()
    050        {
    051        }
    052
    053      @Override
    054      public Collection<String> getCounterGroups()
    055        {
    056        return counters.keySet();
    057        }
    058
    059      @Override
    060      public Collection<String> getCounterGroupsMatching( String regex )
    061        {
    062        Collection<String> counters = getCounterGroups();
    063
    064        Set<String> results = new HashSet<String>();
    065
    066        for( String counter : counters )
    067          {
    068          if( counter.matches( regex ) )
    069            results.add( counter );
    070          }
    071
    072        return Collections.unmodifiableCollection( results );
    073        }
    074
    075      @Override
    076      public Collection<String> getCountersFor( String group )
    077        {
    078        Map<String, Long> groupCollection = counters.get( group );
    079
    080        if( groupCollection == null )
    081          return Collections.emptySet();
    082
    083        return groupCollection.keySet();
    084        }
    085
    086      @Override
    087      public long getCounterValue( Enum counter )
    088        {
    089        Map<String, Long> counterMap = counters.get( counter.getDeclaringClass().getName() );
    090
    091        String counterString = counter.toString();
    092
    093        if( counterMap == null || !counterMap.containsKey( counterString ) )
    094          return 0;
    095
    096        return counterMap.get( counterString );
    097        }
    098
    099      @Override
    100      public long getCounterValue( String group, String counter )
    101        {
    102        Map<String, Long> counterMap = counters.get( group );
    103
    104        if( counterMap == null || !counterMap.containsKey( counter ) )
    105          return 0;
    106
    107        return counterMap.get( counter );
    108        }
    109
    110      public void increment( Enum counter, long amount )
    111        {
    112        increment( counter.getDeclaringClass().getName(), counter.toString(), amount );
    113        }
    114
    115      public void increment( String group, String counter, long amount )
    116        {
    117        Map<String, Long> groupMap = getCreateCounter( group );
    118
    119        Long value = groupMap.get( counter );
    120
    121        if( value == null )
    122          value = 0L;
    123
    124        groupMap.put( counter, value + amount );
    125        }
    126
    127      private Map<String, Long> getCreateCounter( String group )
    128        {
    129        Map<String, Long> counterMap = counters.get( group );
    130
    131        if( counterMap == null )
    132          {
    133          counterMap = new HashMap<String, Long>();
    134          counters.put( group, counterMap );
    135          }
    136
    137        return counterMap;
    138        }
    139
    140      @Override
    141      public void captureDetail()
    142        {
    143        }
    144
    145      @Override
    146      public Collection getChildren()
    147        {
    148        return Collections.emptyList();
    149        }
    150      }