Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backward compatible variant for migration from Scala 2.12 to 2.13 #467

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
Copyright 2012 Twitter, Inc.
Licensed 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 com.twitter.chill.backwardcomp

import com.esotericsoftware.kryo.util.Util.{className, log}
import com.esotericsoftware.kryo.util.{DefaultClassResolver, IdentityObjectIntMap, IntMap}
import com.esotericsoftware.kryo.{Kryo, KryoException, Registration}
import com.esotericsoftware.minlog.Log.{DEBUG, TRACE, trace}
import com.twitter.chill.{Input, Output}

class BackwardCompatibleClassResolver extends DefaultClassResolver {
import BackwardCompatibleClassResolver._

private val fallbackByClass: Map[Class[_], Int] =
Map(
classOf[FallbackToHashSet1] -> 23,
classOf[FallbackToHashSet2] -> 23,
classOf[FallbackToHashMap1] -> 28,
classOf[FallbackToHashMap2] -> 28,
classOf[FallbackToMap11] -> 24,
classOf[FallbackToMap12] -> 24,
classOf[Range.Exclusive] -> 118
)

override def getRegistration(`type`: Class[_]): Registration = {
if (`type` == classOf[Range.Exclusive]) {
getRegistration(118)
} else {
super.getRegistration(`type`)
}
}

override def getRegistration(classID: Int): Registration = {
val registration = super.getRegistration(classID)
if (registration != null) {
fallbackByClass.get(registration.getType) match {
case Some(value) => super.getRegistration(value)
case None =>
if (registration.getType == classOf[Range]) {
new Registration(
classOf[Range.Exclusive],
registration.getSerializer,
registration.getId
)
} else {
registration
}
}
} else {
null
}
}

override def readClass(input: Input): Registration = {
val classID = input.readVarInt(true)
classID match {
case Kryo.NULL =>
if (TRACE || (DEBUG && kryo.getDepth == 1)) log("Read", null)
null
case 1 => // Offset for NAME and NULL.
readName(input)
case _ =>
val registration = getRegistration(classID - 2)
if (registration == null) throw new KryoException("Encountered unregistered class ID: " + (classID - 2))
if (TRACE) trace("kryo", "Read class " + (classID - 2) + ": " + className(registration.getType))

registration
}
}

def readIgnoredClass(input: Input): Unit = {
val classId = input.readVarInt(true)
if (classId == 1) {
val nameId = input.readVarInt(true)
if (nameIdToClass == null) {
nameIdToClass = new IntMap[Class[_]]
}
if (nameIdToClass.get(nameId) == null) {
val name = input.readString()
nameIdToClass.put(nameId, IgnoredClassPlaceholder.getClass)
}
}
}

def writeFakeName[T](output: Output, name: String, placeholderType: Class[_]): Unit = {
output.writeVarInt(1, true)
if (classToNameId != null) {
val nameId = classToNameId.get(placeholderType, -1)
if (nameId != -1) {
output.writeVarInt(nameId, true)
return
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 could we rewrite this without using return?

}
}
// Only write the class name the first time encountered in object graph.
val nameId = nextNameId
nextNameId += 1
if (classToNameId == null) classToNameId = new IdentityObjectIntMap[Class[_]]
classToNameId.put(placeholderType, nameId)
output.writeVarInt(nameId, true)
output.writeString(name)
}
}

object BackwardCompatibleClassResolver {
abstract class FallbackToHashSet1
abstract class FallbackToHashSet2
abstract class FallbackToHashMap1
abstract class FallbackToHashMap2
abstract class FallbackToMap11
abstract class FallbackToMap12

object IgnoredClassPlaceholder
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2012 Twitter, Inc.
Licensed 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 com.twitter.chill.backwardcomp

import com.esotericsoftware.kryo.serializers.FieldSerializer
import com.twitter.chill.{Input, Kryo, Output}

class BackwardCompatibleExclusiveNumericRangeSerializer[T](kryo: Kryo, typ: Class[_]) extends FieldSerializer[T](kryo, typ) {

override def read(kryo: Kryo, input: Input, typ: Class[T]): T = {
val result = create(kryo, input, typ)
kryo.reference(result)

val bitmap0 = input.readByte()
val end = kryo.readClassAndObject(input)
val hashCode = input.readVarInt(false)
val isInclusive = input.readBoolean()
val last = kryo.readClassAndObject(input)
val num1 = kryo.readClassAndObject(input)
val num2 = kryo.readClassAndObject(input)
val numRangeElements = input.readVarInt(false)
val start = kryo.readClassAndObject(input)
val step = kryo.readClassAndObject(input)

for (field <- getFields) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 use while? yields better perf.

field.getField.getName match {
case "bitmap$0" => field.getField.set(result, bitmap0)
case "end" => field.getField.set(result, end)
case "hashCode" => field.getField.set(result, hashCode)
case "isInclusive" => field.getField.set(result, isInclusive)
case "length" => field.getField.set(result, numRangeElements)
case "num" => field.getField.set(result, num1)
case "scala$collection$immutable$NumericRange$$num" => field.getField.set(result, num2)
case "start" => field.getField.set(result, start)
case "step" => field.getField.set(result, step)
}
}

result
}

override def write(kryo: Kryo, output: Output, `object`: T): Unit = {
var length: Option[Int] = None
for (field <- getFields) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎨 use while?

val value = field.getField.get(`object`)
field.getField.getName match {
case "bitmap$0" => output.writeByte(value.asInstanceOf[Byte])
case "end" => kryo.writeClassAndObject(output, value)
case "hashCode" => output.writeVarInt(value.asInstanceOf[Int], false)
case "isInclusive" => output.writeBoolean(value.asInstanceOf[Boolean])
case "length" =>
kryo.writeClassAndObject(output, null) // last
length = Some(value.asInstanceOf[Int]) // storing length to be emitted after the implicits
case "num" => kryo.writeClassAndObject(output, value)
case "scala$collection$immutable$NumericRange$$num" =>
kryo.writeClassAndObject(output, value)
output.writeVarInt(length.get, false)
case "start" => kryo.writeClassAndObject(output, value)
case "step" => kryo.writeClassAndObject(output, value)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
Copyright 2012 Twitter, Inc.
Licensed 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 com.twitter.chill.backwardcomp

import com.esotericsoftware.kryo.serializers.FieldSerializer
import com.twitter.chill.{Input, Kryo, ObjectSerializer, Output}

class BackwardCompatibleJavaWrappersSerializer[T](kryo: Kryo, typ: Class[_]) extends FieldSerializer[T](kryo, typ) {
import BackwardCompatibleJavaWrappersSerializer._

override def read(kryo: Kryo, input: Input, typ: Class[T]): T = {
// Skipping the first serialized field which is assumed to be an $outer object reference
kryo.getClassResolver.asInstanceOf[BackwardCompatibleClassResolver].readIgnoredClass(input)
val refId = input.readVarInt(true)
// Assuming ObjectSerializer which reads no data, so it does not mean if this is the first occurrence or not
super.read(kryo, input, typ)
}

override def write(kryo: Kryo, output: Output, `object`: T): Unit = {
kryo.getClassResolver.asInstanceOf[BackwardCompatibleClassResolver].writeFakeName(output, "scala.collection.convert.Wrappers$", OuterWrapper.getClass)
kryo.writeObjectOrNull(output, OuterWrapper, new ObjectSerializer)
super.write(kryo, output, `object`)
}
}

object BackwardCompatibleJavaWrappersSerializer {
object OuterWrapper
}
Loading