tigrulya-exe commented on code in PR #21028:
URL: https://github.com/apache/flink/pull/21028#discussion_r1023852295


##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/connector/source/CollectionSource.java:
##########
@@ -0,0 +1,219 @@
+/*
+ * 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.flink.streaming.api.connector.source;
+
+import org.apache.flink.annotation.PublicEvolving;
+import org.apache.flink.annotation.VisibleForTesting;
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.connector.source.Boundedness;
+import org.apache.flink.api.connector.source.Source;
+import org.apache.flink.api.connector.source.SourceReader;
+import org.apache.flink.api.connector.source.SourceReaderContext;
+import org.apache.flink.api.connector.source.SplitEnumerator;
+import org.apache.flink.api.connector.source.SplitEnumeratorContext;
+import org.apache.flink.api.connector.source.lib.util.IteratorSourceEnumerator;
+import org.apache.flink.api.connector.source.lib.util.IteratorSourceReader;
+import org.apache.flink.core.io.SimpleVersionedSerializer;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+import org.apache.flink.streaming.api.operators.OutputTypeConfigurable;
+import org.apache.flink.streaming.util.serialization.CollectionSerializer;
+import org.apache.flink.streaming.util.serialization.SimpleObjectSerializer;
+import org.apache.flink.util.Preconditions;
+
+import org.apache.flink.shaded.guava30.com.google.common.collect.Iterables;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Objects;
+
+/**
+ * A {@link Source} implementation that reads data from a collection.
+ *
+ * <p>This source serializes the elements using Flink's type information. That 
way, any object
+ * transport using Java serialization will not be affected by the 
serializability of the elements.
+ *
+ * <p>Note: Parallelism of this source must be 1.
+ */
+@PublicEvolving
+public class CollectionSource<E>
+        implements Source<E, SerializedElementsSplit<E>, 
Collection<SerializedElementsSplit<E>>>,
+                OutputTypeConfigurable<E> {
+    private final transient Iterable<E> elements;
+    private final int elementsCount;
+
+    private byte[] serializedElements;
+    private TypeSerializer<E> serializer;
+
+    public CollectionSource(Iterable<E> elements, TypeSerializer<E> 
serializer) {
+        this.elements = Preconditions.checkNotNull(elements);
+        this.serializer = Preconditions.checkNotNull(serializer);
+        this.elementsCount = Iterables.size(elements);
+        checkIterable(elements, Object.class);
+        serializeElements();
+    }
+
+    public CollectionSource(Iterable<E> elements) {
+        this.elements = Preconditions.checkNotNull(elements);
+        this.elementsCount = Iterables.size(elements);
+        checkIterable(elements, Object.class);
+    }
+
+    @Override
+    public Boundedness getBoundedness() {
+        return Boundedness.BOUNDED;
+    }
+
+    @Override
+    public SourceReader<E, SerializedElementsSplit<E>> createReader(
+            SourceReaderContext readerContext) throws Exception {
+        return new IteratorSourceReader<>(readerContext);
+    }
+
+    @Override
+    public SplitEnumerator<SerializedElementsSplit<E>, 
Collection<SerializedElementsSplit<E>>>
+            
createEnumerator(SplitEnumeratorContext<SerializedElementsSplit<E>> enumContext)
+                    throws Exception {
+        SerializedElementsSplit<E> split =
+                new SerializedElementsSplit<>(serializedElements, 
elementsCount, serializer);
+        return new IteratorSourceEnumerator<>(enumContext, 
Collections.singletonList(split));
+    }
+
+    @Override
+    public SplitEnumerator<SerializedElementsSplit<E>, 
Collection<SerializedElementsSplit<E>>>
+            restoreEnumerator(
+                    SplitEnumeratorContext<SerializedElementsSplit<E>> 
enumContext,
+                    Collection<SerializedElementsSplit<E>> restoredSplits)
+                    throws Exception {
+        Preconditions.checkArgument(
+                restoredSplits.size() <= 1, "Parallelism of CollectionSource 
should be 1");
+        return new IteratorSourceEnumerator<>(enumContext, restoredSplits);
+    }
+
+    @Override
+    public SimpleVersionedSerializer<SerializedElementsSplit<E>> 
getSplitSerializer() {
+        return new ElementsSplitSerializer<>();
+    }
+
+    @Override
+    public SimpleVersionedSerializer<Collection<SerializedElementsSplit<E>>>
+            getEnumeratorCheckpointSerializer() {
+        return new CollectionSerializer<>(new ElementsSplitSerializer<>());
+    }
+
+    private void serializeElements() {
+        Preconditions.checkState(serializer != null, "serializer not set");
+        try (ByteArrayOutputStream baos = new ByteArrayOutputStream();
+                DataOutputViewStreamWrapper wrapper = new 
DataOutputViewStreamWrapper(baos)) {
+            for (E element : elements) {
+                serializer.serialize(element, wrapper);
+            }
+            this.serializedElements = baos.toByteArray();
+        } catch (Exception e) {
+            throw new RuntimeException(
+                    "Serializing the source elements failed: " + 
e.getMessage(), e);
+        }
+    }
+
+    @Override
+    public void setOutputType(TypeInformation<E> outTypeInfo, ExecutionConfig 
executionConfig) {
+        Preconditions.checkState(
+                elements != null,

Review Comment:
   Replaced with `outTypeInfo != null` check



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to