/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.

Licensed 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.tensorflow;

import static org.tensorflow.internal.c_api.global.tensorflow.TFE_OpGetInputLength;
import static org.tensorflow.internal.c_api.global.tensorflow.TFE_OpGetOutputLength;
import static org.tensorflow.internal.c_api.global.tensorflow.TFE_TensorHandleDataType;
import static org.tensorflow.internal.c_api.global.tensorflow.TFE_TensorHandleDim;
import static org.tensorflow.internal.c_api.global.tensorflow.TFE_TensorHandleNumDims;
import static org.tensorflow.internal.c_api.global.tensorflow.TFE_TensorHandleResolve;

import java.util.concurrent.atomic.AtomicReferenceArray;
import org.bytedeco.javacpp.PointerScope;
import org.tensorflow.internal.c_api.TFE_Op;
import org.tensorflow.internal.c_api.TFE_TensorHandle;
import org.tensorflow.internal.c_api.TF_Status;
import org.tensorflow.internal.c_api.TF_Tensor;
import org.tensorflow.tools.Shape;

/**
 * Implementation of an {@link Operation} executed eagerly.
 *
 * <p>EagerOperation instances are valid only as long as the {@link EagerSession} they are a part of
 * is valid. Thus, if {@link EagerSession#close()} has been invoked, then methods on the
 * EagerOperation instance may fail with an {@code IllegalStateException}.
 *
 * <p>EagerOperation instances are thread-safe.
 */
class EagerOperation extends AbstractOperation {

  EagerOperation(
      EagerSession session,
      TFE_Op opNativeHandle,
      TFE_TensorHandle[] outputNativeHandles,
      String type,
      String name) {
    this.session = session;
    this.type = type;
    this.name = name;
    session.attach(this.opHandle = opNativeHandle);
    session.attach(this.outputHandles = outputNativeHandles);
    this.outputTensors = new AtomicReferenceArray<>(outputNativeHandles.length);
  }

  @Override
  public String name() {
    return name;
  }

  @Override
  public String type() {
    return type;
  }

  @Override
  public int numOutputs() {
    return outputHandles.length;
  }

  @Override
  public int outputListLength(final String name) {
    return outputListLength(opHandle, name);
  }

  @Override
  public int inputListLength(final String name) {
    return inputListLength(opHandle, name);
  }

  @Override
  public TFE_TensorHandle getUnsafeNativeHandle(int outputIndex) {
    return outputHandles[outputIndex];
  }

  @Override
  public Shape shape(int outputIndex) {
    // If the tensor of this output has already been resolved, return its shape.
    // Otherwise, retrieve the tensor shape from the native library.
    Tensor<?> tensor = outputTensors.get(outputIndex);
    if (tensor != null) {
      return tensor.shape();
    }
    TFE_TensorHandle outputNativeHandle = getUnsafeNativeHandle(outputIndex);
    long[] shape = new long[numDims(outputNativeHandle)];
    for (int i = 0; i < shape.length; ++i) {
      shape[i] = dim(outputNativeHandle, i);
    }
    return Shape.of(shape);
  }

  @Override
  public DataType<?> dtype(int outputIndex) {
    // If the tensor of this output has already been resolved, return its datatype.
    // Otherwise, retrieve the tensor datatype from the native library.
    Tensor<?> tensor = outputTensors.get(outputIndex);
    if (tensor != null) {
      return tensor.dataType();
    }
    TFE_TensorHandle outputNativeHandle = getUnsafeNativeHandle(outputIndex);
    return DataTypes.fromNativeCode(dataType(outputNativeHandle));
  }

  @Override
  public Tensor<?> tensor(int outputIndex) {
    Tensor<?> tensor = outputTensors.get(outputIndex);
    if (tensor == null) {
      tensor = resolveTensor(outputIndex);
    }
    return tensor;
  }

  private final EagerSession session;
  private final String type;
  private final String name;
  private final AtomicReferenceArray<Tensor<?>> outputTensors;

  private Tensor<?> resolveTensor(int outputIndex) {
    // Take an optimistic approach, where we attempt to resolve the output tensor without locking.
    // If another thread has resolved it meanwhile, release our copy and reuse the existing one
    // instead.
    TF_Tensor tensorNativeHandle = resolveTensorHandle(getUnsafeNativeHandle(outputIndex));
    Tensor<?> tensor = Tensor.fromHandle(tensorNativeHandle, session);
    if (!outputTensors.compareAndSet(outputIndex, null, tensor)) {
      session.detach(tensorNativeHandle);
      tensor = outputTensors.get(outputIndex);
    }
    tensorNativeHandle.releaseReference();
    return tensor;
  }

  private TFE_Op opHandle;
  private final TFE_TensorHandle[] outputHandles;

  private static void requireOp(TFE_Op handle) {
    if (handle == null || handle.isNull()) {
      throw new IllegalStateException("Eager session has been closed");
    }
  }

  private static void requireTensorHandle(TFE_TensorHandle handle) {
    if (handle == null || handle.isNull()) {
      throw new IllegalStateException("Eager session has been closed");
    }
  }

  private static TF_Tensor resolveTensorHandle(TFE_TensorHandle handle) {
    requireTensorHandle(handle);
    try (PointerScope scope = new PointerScope()) {
      TF_Status status = TF_Status.newStatus();
      TF_Tensor tensor = TFE_TensorHandleResolve(handle, status).withDeallocator().retainReference();
      status.throwExceptionIfNotOK();
      return tensor;
    }
  }

  private static int outputListLength(TFE_Op handle, String name) {
    requireOp(handle);
    try (PointerScope scope = new PointerScope()) {
      TF_Status status = TF_Status.newStatus();
      int length = TFE_OpGetOutputLength(handle, name, status);
      status.throwExceptionIfNotOK();
      return length;
    }
  }

  private static int inputListLength(TFE_Op handle, String name) {
    requireOp(handle);
    try (PointerScope scope = new PointerScope()) {
      TF_Status status = TF_Status.newStatus();
      int length = TFE_OpGetInputLength(handle, name, status);
      status.throwExceptionIfNotOK();
      return length;
    }
  }

  private static int dataType(TFE_TensorHandle handle) {
    requireTensorHandle(handle);
    return TFE_TensorHandleDataType(handle);
  }

  private static int numDims(TFE_TensorHandle handle) {
    requireTensorHandle(handle);
    try (PointerScope scope = new PointerScope()) {
      TF_Status status = TF_Status.newStatus();
      int numDims = TFE_TensorHandleNumDims(handle, status);
      status.throwExceptionIfNotOK();
      return numDims;
    }
  }

  private static long dim(TFE_TensorHandle handle, int index) {
    requireTensorHandle(handle);
    try (PointerScope scope = new PointerScope()) {
      TF_Status status = TF_Status.newStatus();
      long dim = TFE_TensorHandleDim(handle, index, status);
      status.throwExceptionIfNotOK();
      return dim;
    }
  }
}

@@ -2212,41 +2212,71 @@
     
-session.attach(
+this.opHandle = opNativeHandle;%0A    
 this.o
-p
+utput
 Handle
+s
  = o
-p
+utput
 Nati
@@ -2279,25 +2279,25 @@
 NativeHandle
-)
+s
 ;%0A    sessio
@@ -2305,37 +2305,52 @@
 .attach(
-this.outputHandles = 
+opNativeHandle);%0A    session.attach(
 outputNa

Fix nits