/*
 * 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 org.apache.pig.backend.hadoop.executionengine.shims;

import org.apache.hadoop.mapreduce.Counter;
import org.apache.hadoop.mapreduce.TaskInputOutputContext;
import org.apache.pig.backend.hadoop.executionengine.fetch.FetchContext;
import org.apache.tez.common.counters.TezCounter;
import org.apache.tez.runtime.api.TezProcessorContext;

public class TaskContext<T> {
    private T context;

    public TaskContext(T context) {
        this.context = context;
    }

    public T get() {
        return context;
    }

    public Counter getCounter(Enum<?> name) {
        Counter counter = null;
        if (context == null) {
            return counter;
        }
        if (context instanceof TaskInputOutputContext) {
            TaskInputOutputContext<?,?,?,?> taskContext = (TaskInputOutputContext<?,?,?,?>) context;
            counter = taskContext.getCounter(name);
        } else if (context instanceof FetchContext) {
            FetchContext fetchContext = (FetchContext) context;
            counter = fetchContext.getCounter(name);
        }
        return counter;
    }

    public Counter getCounter(String group, String name) {
        Counter counter = null;
        if (context == null) {
            return counter;
        }
        if (context instanceof TaskInputOutputContext) {
            TaskInputOutputContext<?,?,?,?> taskContext = (TaskInputOutputContext<?,?,?,?>) context;
            counter = taskContext.getCounter(group, name);
        } else if (context instanceof FetchContext) {
            FetchContext fetchContext = (FetchContext) context;
            counter = fetchContext.getCounter(group, name);
        }
        return counter;
    }

    public boolean incrCounter(Enum<?> name, long delta) {
        if (context == null) {
            return false;
        }
        if (context instanceof TaskInputOutputContext) {
            TaskInputOutputContext<?,?,?,?> taskContext = (TaskInputOutputContext<?,?,?,?>) context;
            Counter counter = taskContext.getCounter(name);
            counter.increment(delta);
            return true;
        } else if (context instanceof FetchContext) {
            FetchContext fetchContext = (FetchContext) context;
            Counter counter = fetchContext.getCounter(name);
            counter.increment(delta);
            return true;
        } else if (context instanceof TezProcessorContext) {
            TezProcessorContext tezContext = (TezProcessorContext) context;
            TezCounter counter = tezContext.getCounters().findCounter(name);
            counter.increment(delta);
            return true;
        }
        return false;
    }

    public boolean incrCounter(String group, String name, long delta) {
        if (context == null) {
            return false;
        }
        if (context instanceof TaskInputOutputContext) {
            TaskInputOutputContext<?,?,?,?> taskContext = (TaskInputOutputContext<?,?,?,?>) context;
            Counter counter = taskContext.getCounter(group, name);
            counter.increment(delta);
            return true;
        } else if (context instanceof FetchContext) {
            FetchContext fetchContext = (FetchContext) context;
            Counter counter = fetchContext.getCounter(group, name);
            counter.increment(delta);
            return true;
        } else if (context instanceof TezProcessorContext) {
            TezProcessorContext tezContext = (TezProcessorContext) context;
            TezCounter counter = tezContext.getCounters().getGroup(group).findCounter(name);
            counter.increment(delta);
            return true;
        }
        return false;
    }

    public void progress() {
        if (context == null) {
            return;
        }
        if (context instanceof TaskInputOutputContext) {
            TaskInputOutputContext<?,?,?,?> taskContext = (TaskInputOutputContext<?,?,?,?>) context;
            taskContext.progress();
        }
    }

    public float getProgress() {
        if (context == null) {
            return 0f;
        }
        if (context instanceof TaskInputOutputContext) {
            TaskInputOutputContext<?,?,?,?> taskContext = (TaskInputOutputContext<?,?,?,?>) context;
            return taskContext.getProgress();
        }
        return 0f;
    }

    public void setStatus(String status) {
        if (context == null) {
            return;
        }
        if (context instanceof TaskInputOutputContext) {
            TaskInputOutputContext<?,?,?,?> taskContext = (TaskInputOutputContext<?,?,?,?>) context;
            taskContext.setStatus(status);
        }
    }
}

@@ -1040,113 +1040,8 @@
 ext;
-%0Aimport org.apache.tez.common.counters.TezCounter;%0Aimport org.apache.tez.runtime.api.TezProcessorContext;
 %0A%0Apu
@@ -3037,285 +3037,8 @@
 ue;%0A
-        %7D else if (context instanceof TezProcessorContext) %7B%0A            TezProcessorContext tezContext = (TezProcessorContext) context;%0A            TezCounter counter = tezContext.getCounters().findCounter(name);%0A            counter.increment(delta);%0A            return true;%0A
     
@@ -3752,301 +3752,8 @@
 ue;%0A
-        %7D else if (context instanceof TezProcessorContext) %7B%0A            TezProcessorContext tezContext = (TezProcessorContext) context;%0A            TezCounter counter = tezContext.getCounters().getGroup(group).findCounter(name);%0A            counter.increment(delta);%0A            return true;%0A
     

Refactor PigStatusReporter and PigLogger for non-MR execution engine (Remove tez-related code from TaskContext in hadoop23 shum. It was committed accidentally.)