Github user StephanEwen commented on a diff in the pull request:

    https://github.com/apache/flink/pull/848#discussion_r32915549
  
    --- Diff: 
flink-staging/flink-streaming/flink-streaming-core/src/main/java/org/apache/flink/streaming/api/functions/source/FromElementsFunction.java
 ---
    @@ -17,37 +17,81 @@
     
     package org.apache.flink.streaming.api.functions.source;
     
    -import java.util.Arrays;
    -import java.util.Collection;
    +import org.apache.flink.api.common.typeutils.TypeSerializer;
    +import org.apache.flink.core.memory.DataInputView;
    +import org.apache.flink.core.memory.InputViewDataInputStreamWrapper;
    +import org.apache.flink.core.memory.OutputViewDataOutputStreamWrapper;
    +
    +import java.io.ByteArrayInputStream;
    +import java.io.ByteArrayOutputStream;
    +import java.io.DataInputStream;
    +import java.io.DataOutputStream;
    +import java.io.IOException;
     import java.util.Iterator;
     
     public class FromElementsFunction<T> implements SourceFunction<T> {
        
        private static final long serialVersionUID = 1L;
     
    -   private Iterable<T> iterable;
    +   private final TypeSerializer<T> serializer;
    +   private final byte[] elements;
     
        private volatile boolean isRunning = true;
     
    -   public FromElementsFunction(T... elements) {
    -           this.iterable = Arrays.asList(elements);
    -   }
    +   public FromElementsFunction(TypeSerializer<T> serializer, final T... 
elements) {
    +           this(serializer, new Iterable<T>() {
    +                   @Override
    +                   public Iterator<T> iterator() {
    +                           return new Iterator<T>() {
    +                                   int index = 0;
    +
    +                                   @Override
    +                                   public boolean hasNext() {
    +                                           return index < elements.length;
    +                                   }
    +
    +                                   @Override
    +                                   public T next() {
    +                                           return elements[index++];
    +                                   }
     
    -   public FromElementsFunction(Collection<T> elements) {
    -           this.iterable = elements;
    +                                   @Override
    +                                   public void remove() {
    +                                           throw new 
UnsupportedOperationException();
    +                                   }
    +                           };
    +                   }
    +           });
        }
     
    -   public FromElementsFunction(Iterable<T> elements) {
    -           this.iterable = elements;
    +   public FromElementsFunction(TypeSerializer<T> serializer, Iterable<T> 
elements) {
    +           ByteArrayOutputStream baos = new ByteArrayOutputStream();
    +           OutputViewDataOutputStreamWrapper wrapper = new 
OutputViewDataOutputStreamWrapper(new DataOutputStream(baos));
    +
    +           try {
    +                   for (T element : elements) {
    +                           serializer.serialize(element, wrapper);
    +                   }
    +           } catch (IOException e) {
    +                   // ByteArrayOutputStream doesn't throw IOExceptions 
when written to
    +           }
    +           // closing the DataOutputStream would just flush the 
ByteArrayOutputStream, which in turn doesn't do anything.
    +
    +           this.serializer = serializer;
    +           this.elements = baos.toByteArray();
        }
     
        @Override
        public void run(SourceContext<T> ctx) throws Exception {
    -           Iterator<T> it = iterable.iterator();
    +           T value = serializer.createInstance();
    --- End diff --
    
    Let's not use object reusing mode by default. It is not guaranteed to 
always work (some abstract types are not instantiable) and should only work on 
user-requested activation. Use `value = serializer.deserialize(input);` instead.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to