/*
 * 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.beam.runners.spark.structuredstreaming.translation.helpers;

import static scala.collection.JavaConversions.asScalaBuffer;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.transforms.windowing.GlobalWindow;
import org.apache.beam.sdk.util.WindowedValue;
import org.apache.spark.api.java.function.MapFunction;
import org.apache.spark.sql.Row;
import org.apache.spark.sql.catalyst.InternalRow;

/** Helper functions for working with {@link Row}. */
public final class RowHelpers {

  /**
   * A Spark {@link MapFunction} for extracting a {@link WindowedValue} from a Row in which the
   * {@link WindowedValue} was serialized to bytes using its {@link
   * WindowedValue.WindowedValueCoder}.
   *
   * @param <T> The type of the object.
   * @return A {@link MapFunction} that accepts a {@link Row} and returns its {@link WindowedValue}.
   */
  public static <T> MapFunction<Row, WindowedValue<T>> extractWindowedValueFromRowMapFunction(
      Coder<T> coder) {
    return (MapFunction<Row, WindowedValue<T>>)
        value -> {
          // there is only one value put in each Row by the InputPartitionReader
          byte[] bytes = (byte[]) value.get(0);
          WindowedValue.FullWindowedValueCoder<T> windowedValueCoder =
              WindowedValue.FullWindowedValueCoder.of(coder, GlobalWindow.Coder.INSTANCE);
          return windowedValueCoder.decode(new ByteArrayInputStream(bytes));
        };
  }

  /**
   * Serialize a windowedValue to bytes using windowed {@link WindowedValue.FullWindowedValueCoder}
   * and stores it an InternalRow.
   */
  public static <T> InternalRow storeWindowedValueInRow(
      WindowedValue<T> windowedValue, Coder<T> coder) {
    List<Object> list = new ArrayList<>();
    // serialize the windowedValue to bytes array to comply with dataset binary schema
    WindowedValue.FullWindowedValueCoder<T> windowedValueCoder =
        WindowedValue.FullWindowedValueCoder.of(coder, GlobalWindow.Coder.INSTANCE);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {
      windowedValueCoder.encode(windowedValue, byteArrayOutputStream);
      byte[] bytes = byteArrayOutputStream.toByteArray();
      list.add(bytes);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
    return InternalRow.apply(asScalaBuffer(list).toList());
  }

  /**
   * A Spark {@link MapFunction} for extracting an Object from a Row was serialized to bytes using
   * kryo.
   *
   * @param <T> The type of the object.
   * @return A {@link MapFunction} that accepts a {@link Row} and returns its {@link WindowedValue}.
   */
  public static <T> MapFunction<Row, T> extractObjectFromRowMapFunction() {
    return (MapFunction<Row, T>) RowHelpers::extractObjectFromRow;
  }

  /** Extracts an Object from a Row was serialized to bytes using kryo. */
  @SuppressWarnings("TypeParameterUnusedInFormals")
  public static <T> T extractObjectFromRow(Row value) {
    // there is only one value put in each Row by the InputPartitionReader
    byte[] bytes = (byte[]) value.get(0);
    if (bytes == null) {
      return null;
    }
    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
    Kryo kryo = new Kryo();
    Input input = new Input(inputStream);
    @SuppressWarnings("unchecked")
    T object = (T) kryo.readClassAndObject(input);
    input.close();
    return object;
  }
}

@@ -3407,1044 +3407,7 @@
 %0A  %7D
-%0A%0A  /**%0A   * A Spark %7B@link MapFunction%7D for extracting an Object from a Row was serialized to bytes using%0A   * kryo.%0A   *%0A   * @param %3CT%3E The type of the object.%0A   * @return A %7B@link MapFunction%7D that accepts a %7B@link Row%7D and returns its %7B@link WindowedValue%7D.%0A   */%0A  public static %3CT%3E MapFunction%3CRow, T%3E extractObjectFromRowMapFunction() %7B%0A    return (MapFunction%3CRow, T%3E) RowHelpers::extractObjectFromRow;%0A  %7D%0A%0A  /** Extracts an Object from a Row was serialized to bytes using kryo. */%0A  @SuppressWarnings(%22TypeParameterUnusedInFormals%22)%0A  public static %3CT%3E T extractObjectFromRow(Row value) %7B%0A    // there is only one value put in each Row by the InputPartitionReader%0A    byte%5B%5D bytes = (byte%5B%5D) value.get(0);%0A    if (bytes == null) %7B%0A      return null;%0A    %7D%0A    ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);%0A    Kryo kryo = new Kryo();%0A    Input input = new Input(inputStream);%0A    @SuppressWarnings(%22unchecked%22)%0A    T object = (T) kryo.readClassAndObject(input);%0A    input.close();%0A    return object;%0A  %7D
 %0A%7D%0A

Clean not more needed RowHelpers