X-czh commented on code in PR #24845:
URL: https://github.com/apache/flink/pull/24845#discussion_r1619710677


##########
flink-core/src/main/java/org/apache/flink/api/common/typeutils/base/SetSerializer.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.api.common.typeutils.base;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A serializer for {@link java.util.Set}. The serializer relies on an element 
serializer for the
+ * serialization of the set's elements.
+ *
+ * <p>The serialization format for the set is as follows: four bytes for the 
length of the set,
+ * followed by the serialized representation of each element.
+ *
+ * @param <T> The type of element in the set.
+ */
+@Internal
+public final class SetSerializer<T> extends TypeSerializer<Set<T>> {
+
+    private static final long serialVersionUID = 1L;
+
+    /** The serializer for the elements of the set. */
+    private final TypeSerializer<T> elementSerializer;
+
+    /**
+     * Creates a set serializer that uses the given serializer to serialize 
the set's elements.
+     *
+     * @param elementSerializer The serializer for the elements of the set
+     */
+    public SetSerializer(TypeSerializer<T> elementSerializer) {
+        this.elementSerializer = checkNotNull(elementSerializer);
+    }
+
+    // ------------------------------------------------------------------------
+    //  SetSerializer specific properties
+    // ------------------------------------------------------------------------
+
+    /**
+     * Gets the serializer for the elements of the set.
+     *
+     * @return The serializer for the elements of the set
+     */
+    public TypeSerializer<T> getElementSerializer() {
+        return elementSerializer;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Type Serializer implementation
+    // ------------------------------------------------------------------------
+
+    @Override
+    public boolean isImmutableType() {
+        return false;
+    }
+
+    @Override
+    public TypeSerializer<Set<T>> duplicate() {
+        TypeSerializer<T> duplicateElement = elementSerializer.duplicate();
+        return duplicateElement == elementSerializer ? this : new 
SetSerializer<>(duplicateElement);
+    }
+
+    @Override
+    public Set<T> createInstance() {
+        return new HashSet<>(0);
+    }
+
+    @Override
+    public Set<T> copy(Set<T> from) {
+        Set<T> newSet = new HashSet<>(from.size());
+        for (T element : from) {
+            newSet.add(elementSerializer.copy(element));
+        }
+        return newSet;
+    }
+
+    @Override
+    public Set<T> copy(Set<T> from, Set<T> reuse) {
+        return copy(from);
+    }
+
+    @Override
+    public int getLength() {
+        return -1; // var length
+    }
+
+    @Override
+    public void serialize(Set<T> set, DataOutputView target) throws 
IOException {
+        final int size = set.size();
+        target.writeInt(size);
+        for (T element : set) {
+            elementSerializer.serialize(element, target);
+        }
+    }
+
+    @Override
+    public Set<T> deserialize(DataInputView source) throws IOException {
+        final int size = source.readInt();
+        final Set<T> set = new HashSet<>(size);

Review Comment:
   We intentionally only support de/serializing an object declared with the 
interface `Set` type, and do not provide any additional guarantee on ordering 
or other advanced properties to reduce the complexity. We adopt the same 
strategy for supporting `Map` and `List`, and have stated it explicitly in the 
document:
   
   > To utilize it, you need to declare the collection type with:
   >
   > 1. Concrete type arguments: e.g. `List<String>` but not `List`, `List<T>`, 
or `List<?>`, as Flink needs them to dispatch
   >    serialization of the element types.
   > 2. Interface types: e.g. `List<String>` but not `LinkedList<String>`, as 
Flink does not preserve the underlying
   >    implementation types across serialization.



-- 
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