-
Notifications
You must be signed in to change notification settings - Fork 156
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
vigoo
wants to merge
4
commits into
twitter:develop
Choose a base branch
from
vigoo:backward-compatible-213
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...src/main/scala-2.13+/com/twitter/chill/backwardcomp/BackwardCompatibleClassResolver.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
// 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 | ||
} |
74 changes: 74 additions & 0 deletions
74
...3+/com/twitter/chill/backwardcomp/BackwardCompatibleExclusiveNumericRangeSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎨 use |
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎨 use |
||
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) | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...scala-2.13+/com/twitter/chill/backwardcomp/BackwardCompatibleJavaWrappersSerializer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
?