You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/02/19 10:32:37 UTC

[GitHub] aljoscha commented on a change in pull request #7734: [FLINK-11334][core] Migrate EnumValueSerializer to use new serialization compatibility abstractions

aljoscha commented on a change in pull request #7734: [FLINK-11334][core] Migrate EnumValueSerializer to use new serialization compatibility abstractions
URL: https://github.com/apache/flink/pull/7734#discussion_r257978040
 
 

 ##########
 File path: flink-scala/src/main/scala/org/apache/flink/api/scala/typeutils/ScalaEnumSerializerSnapshot.scala
 ##########
 @@ -0,0 +1,136 @@
+/*
+ * 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.scala.typeutils
+
+import java.io.IOException
+
+import org.apache.flink.api.common.typeutils.{TypeSerializer, TypeSerializerSchemaCompatibility, TypeSerializerSnapshot}
+import org.apache.flink.api.java.typeutils.runtime.{DataInputViewStream, DataOutputViewStream}
+import org.apache.flink.core.memory.{DataInputView, DataOutputView}
+import org.apache.flink.util.{InstantiationUtil, Preconditions}
+
+import scala.collection.mutable.ListBuffer
+
+class ScalaEnumSerializerSnapshot[E <: Enumeration]
+  extends TypeSerializerSnapshot[E#Value] {
+
+  var enumClass: Class[E] = _
+  var previousEnumConstants: List[(String, Int)] = _
+
+  def this(enum: E) = {
+    this()
+    this.enumClass = Preconditions.checkNotNull(enum).getClass.asInstanceOf[Class[E]]
+    this.previousEnumConstants = enum.values.toList.map(x => (x.toString, x.id))
+  }
+
+  def this(enumClass: Class[E], previousEnumConstants: List[(String, Int)]) = {
+    this()
+    this.enumClass = Preconditions.checkNotNull(enumClass)
+    this.previousEnumConstants = Preconditions.checkNotNull(previousEnumConstants)
+  }
+
+  override def getCurrentVersion: Int = ScalaEnumSerializerSnapshot.VERSION
+
+  override def writeSnapshot(out: DataOutputView): Unit = {
+    val outViewWrapper = new DataOutputViewStream(out)
+    try {
+      out.writeUTF(enumClass.getName)
+
+      out.writeInt(previousEnumConstants.length)
+      for ((name, idx) <- previousEnumConstants) {
+        out.writeUTF(name)
+        out.writeInt(idx)
+      }
+    } finally if (outViewWrapper != null) outViewWrapper.close()
+  }
+
+  override def readSnapshot(
+    readVersion: Int, in: DataInputView, userCodeClassLoader: ClassLoader): Unit = {
+    val inViewWrapper = new DataInputViewStream(in)
+    try {
+      if (readVersion == 1) {
+        enumClass = InstantiationUtil.deserializeObject(
+          inViewWrapper, userCodeClassLoader)
+
+        // read null from input stream
+        InstantiationUtil.deserializeObject(inViewWrapper, userCodeClassLoader)
+        previousEnumConstants = List()
+      } else if (readVersion >= 2) {
+        enumClass = Class.forName(
+          in.readUTF(), true, userCodeClassLoader).asInstanceOf[Class[E]]
+
+        val length = in.readInt()
+        val listBuffer = ListBuffer[(String, Int)]()
+
+        for (_ <- 0 until length) {
+          val name = in.readUTF()
+          val idx = in.readInt()
+          listBuffer += ((name, idx))
+        }
+
+        previousEnumConstants = listBuffer.toList
+      } else {
+        throw new IOException(
+          s"Cannot deserialize ${getClass.getSimpleName} with version $readVersion.")
+      }
+    } catch {
+      case e: ClassNotFoundException =>
+        throw new IOException("The requested enum class cannot be found in classpath.", e)
+    }
+    finally if (inViewWrapper != null) inViewWrapper.close()
+  }
+
+  override def restoreSerializer(): TypeSerializer[E#Value] = {
 
 Review comment:
   This does not create a serializer but tries to create an instance of the enum (which I think would fail). I'm wondering why this method is never called in the tests.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services