/**
 * 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
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * 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 org.apache.storm.metric;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Multimap;
import com.google.common.collect.Table;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.apache.storm.metric.api.IMetricsConsumer;
import org.apache.storm.task.IErrorReporter;
import org.apache.storm.task.TopologyContext;

public class FakeMetricConsumer implements IMetricsConsumer {

    public static final Table<String, String, Multimap<Integer, Object>> buffer = HashBasedTable.create();

    public static Map<Integer, Collection<Object>> getTaskIdToBuckets(String componentName, String metricName) {
        synchronized (buffer) {
            Multimap<Integer, Object> taskIdToBuckets = buffer.get(componentName, metricName);
            return (null != taskIdToBuckets) ? taskIdToBuckets.asMap() : null;
        }
    }

    @Override
    public void prepare(Map<String, Object> topoConf, Object registrationArgument, TopologyContext context, IErrorReporter errorReporter) {
        synchronized (buffer) {
            buffer.clear();
        }
    }

    @Override
    public void handleDataPoints(TaskInfo taskInfo, Collection<DataPoint> dataPoints) {
        synchronized (buffer) {
            for (DataPoint dp : dataPoints) {
                for (Map.Entry<String, Object> entry : expandComplexDataPoint(dp).entrySet()) {
                    String metricName = entry.getKey();
                    Multimap<Integer, Object> taskIdToBucket = buffer.get(taskInfo.srcComponentId, metricName);
                    if (null == taskIdToBucket) {
                        taskIdToBucket = ArrayListMultimap.create();
                        taskIdToBucket.put(taskInfo.srcTaskId, entry.getValue());
                    } else {
                        taskIdToBucket.get(taskInfo.srcTaskId).add(entry.getValue());
                    }
                    buffer.put(taskInfo.srcComponentId, metricName, taskIdToBucket);
                }
            }
        }
    }

    @Override
    public void cleanup() {
        synchronized (buffer) {
            buffer.clear();
        }
    }

    private Map<String, Object> expandComplexDataPoint(DataPoint dp) {
        Map<String, Object> expanded = new HashMap<>();
        if (dp.value instanceof Map) {
            for (Map.Entry entry : ((Map<Object, Object>) dp.value).entrySet()) {
                expanded.put(dp.name + "/" + entry.getKey(), entry.getValue());
            }
        } else {
            expanded.put(dp.name, dp.value);
        }
        return expanded;
    }
}

@@ -1365,22 +1365,22 @@
 bject%3E%3E 
-buffer
+BUFFER
  = HashB
@@ -1527,38 +1527,38 @@
   synchronized (
-buffer
+BUFFER
 ) %7B%0A            
@@ -1601,22 +1601,22 @@
 ckets = 
-buffer
+BUFFER
 .get(com
@@ -1687,16 +1687,30 @@
 kets) ? 
+new HashMap%3C%3E(
 taskIdTo
@@ -1724,16 +1724,17 @@
 .asMap()
+)
  : null;
@@ -1919,38 +1919,38 @@
   synchronized (
-buffer
+BUFFER
 ) %7B%0A            
@@ -1941,38 +1941,38 @@
 ) %7B%0A            
-buffer
+BUFFER
 .clear();%0A      
@@ -2098,38 +2098,38 @@
   synchronized (
-buffer
+BUFFER
 ) %7B%0A            
@@ -2377,22 +2377,22 @@
 ucket = 
-buffer
+BUFFER
 .get(tas
@@ -2784,22 +2784,22 @@
         
-buffer
+BUFFER
 .put(tas
@@ -2962,22 +2962,22 @@
 onized (
-buffer
+BUFFER
 ) %7B%0A    
@@ -2988,14 +2988,14 @@
     
-buffer
+BUFFER
 .cle

Copy map before returning in FakeMetricConsumer, since returning a view of the map is unsafe due to concurrent modification