/*
 * 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.intel.hibench.streambench.storm.trident;

import com.intel.hibench.streambench.common.metrics.KafkaReporter;
import com.intel.hibench.streambench.common.metrics.LatencyReporter;
import com.intel.hibench.streambench.storm.spout.KafkaSpoutFactory;
import com.intel.hibench.streambench.storm.topologies.SingleTridentSpoutTops;
import com.intel.hibench.streambench.storm.trident.functions.Parser;
import com.intel.hibench.streambench.storm.util.StormBenchConfig;
import org.apache.storm.kafka.trident.OpaqueTridentKafkaSpout;
import org.apache.storm.trident.TridentTopology;
import org.apache.storm.trident.operation.Aggregator;
import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.operation.TridentOperationContext;
import org.apache.storm.trident.spout.ITridentDataSource;
import org.apache.storm.trident.tuple.TridentTuple;
import org.apache.storm.tuple.Fields;
import org.apache.storm.tuple.Values;

import java.util.Map;

public class TridentWordcount extends SingleTridentSpoutTops {

  public TridentWordcount(StormBenchConfig config) {
    super(config);
  }

  @Override
  public TridentTopology createTopology() {
    ITridentDataSource source = KafkaSpoutFactory.getTridentSpout(config, true);

    TridentTopology topology = new TridentTopology();
    topology.newStream("kafka", source)
        .each(new Fields("str"), new Parser(), new Fields("ip", "time"))
        .project(new Fields("ip", "time"))
        .parallelismHint(config.spoutThreads)
        .groupBy(new Fields("ip"))
        .aggregate(new Fields("ip", "time"), new Count(config), new Fields("word", "count"))
        .parallelismHint(config.boltThreads);
    return topology;
  }

  private static class Count implements Aggregator<Count.State> {

    private final StormBenchConfig config;
    private LatencyReporter reporter = null;

    static class State {
      String ip;
      long count = 0;

    }

    Count(StormBenchConfig config) {
      this.config = config;
    }

    @Override
    public void prepare(Map conf, TridentOperationContext context) {
      this.reporter = new KafkaReporter(config.reporterTopic, config.brokerList);
    }

    @Override
    public State init(Object o, TridentCollector tridentCollector) {
      return new State();
    }

    @Override
    public void aggregate(State state, TridentTuple tridentTuple, TridentCollector tridentCollector) {
      state.ip = tridentTuple.getString(0);
      state.count++;
      tridentCollector.emit(new Values(state.ip, state.count));
      reporter.report(tridentTuple.getLong(0), System.currentTimeMillis());
    }

    @Override
    public void complete(State state, TridentCollector tridentCollector) {
    }

    @Override
    public void cleanup() {
    }
  }
}

@@ -1270,71 +1270,8 @@
 ig;%0A
-import org.apache.storm.kafka.trident.OpaqueTridentKafkaSpout;%0A
 impo
@@ -1357,16 +1357,23 @@
 eration.
+Reducer
 Aggregat
@@ -1378,135 +1378,8 @@
 ator
-;%0Aimport org.apache.storm.trident.operation.TridentCollector;%0Aimport org.apache.storm.trident.operation.TridentOperationContext
 ;%0Aim
@@ -1434,16 +1434,72 @@
 Source;%0A
+import org.apache.storm.trident.testing.MemoryMapState;%0A
 import o
@@ -1583,69 +1583,8 @@
 lds;
-%0Aimport org.apache.storm.tuple.Values;%0A%0Aimport java.util.Map;
 %0A%0Apu
@@ -2160,17 +2160,27 @@
        .
-a
+persistentA
 ggregate
@@ -2180,24 +2180,54 @@
 gregate(new 
+MemoryMapState.Factory(), new 
 Fields(%22ip%22,
@@ -2254,16 +2254,28 @@
 config),
+%0A           
  new Fie
@@ -2408,16 +2408,23 @@
 lements 
+Reducer
 Aggregat
@@ -2535,80 +2535,8 @@
 l;%0A%0A
-    static class State %7B%0A      String ip;%0A      long count = 0;%0A%0A    %7D%0A%0A
     
@@ -2632,62 +2632,19 @@
 lic 
-void prepare(Map conf, TridentOperationContext context
+State init(
 ) %7B%0A
@@ -2729,98 +2729,8 @@
 t);%0A
-    %7D%0A%0A    @Override%0A    public State init(Object o, TridentCollector tridentCollector) %7B%0A
     
@@ -2787,21 +2787,19 @@
 lic 
-void aggregat
+State reduc
 e(St
@@ -2838,43 +2838,8 @@
 uple
-, TridentCollector tridentCollector
 ) %7B%0A
@@ -2907,72 +2907,8 @@
 ++;%0A
-      tridentCollector.emit(new Values(state.ip, state.count));%0A
     
@@ -2946,17 +2946,17 @@
 getLong(
-0
+1
 ), Syste
@@ -2987,148 +2987,94 @@
     
-%7D%0A%0A    @Override%0A    public void complete(State state, TridentCollector tridentCollector) %7B%0A    %7D%0A%0A    @Override%0A    public void cleanup() %7B
+  return state;%0A    %7D%0A%0A    static class State %7B%0A      String ip;%0A      long count = 0;
 %0A   

fix trident wordcount