-
-
Save dirkgr/bc0e8b547015db9e67c7 to your computer and use it in GitHub Desktop.
Kryo Bug?
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
import java.nio.file.{Files, Paths} | |
import com.esotericsoftware.kryo.io.{Input, Output} | |
import com.twitter.chill.ScalaKryoInstantiator | |
import scala.collection.mutable | |
import scala.reflect.ClassTag | |
object ScoptBug extends App { | |
val kryo = (new ScalaKryoInstantiator).setRegistrationRequired(false).newKryo() | |
// save the map | |
val myMap = mutable.HashMap[String, Set[String]]().withDefaultValue(Set()) | |
myMap.update("foo", Set("bar")) | |
println(myMap.getClass) | |
println(myMap("squirrel")) | |
println() | |
val cacheFile = Paths.get( | |
System.getProperty("java.io.tmpdir"), | |
s"${this.getClass.getSimpleName}-CachedLoader-${myMap.hashCode.toHexString}.chill") | |
val os = Files.newOutputStream(cacheFile) | |
try { | |
kryo.writeObject(new Output(os), myMap) | |
} finally { | |
os.close() | |
} | |
// load the map | |
val is = Files.newInputStream(cacheFile) | |
val result = try { | |
kryo.readObject(new Input(is), myMap.getClass) | |
} finally { | |
is.close() | |
} | |
println(result.getClass) | |
println(result("squirrel")) | |
println() | |
// try to cast it | |
val withDefaultMap = result.asInstanceOf[scala.collection.mutable.Map.WithDefault[String, Set[String]]] | |
println(withDefaultMap.getClass) | |
println(withDefaultMap("squirrel")) | |
println() | |
Files.deleteIfExists(cacheFile) | |
} |
Full output:
class scala.collection.mutable.Map$WithDefault
Set()
class scala.collection.mutable.HashMap
Exception in thread "main" java.util.NoSuchElementException: key not found: squirrel
at scala.collection.MapLike$class.default(MapLike.scala:228)
at scala.collection.AbstractMap.default(Map.scala:59)
at scala.collection.mutable.HashMap.apply(HashMap.scala:65)
at ScoptBug$.delayedEndpoint$ScoptBug$1(ScoptBug.scala:39)
at ScoptBug$delayedInit$body.apply(ScoptBug.scala:10)
at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.App$$anonfun$main$1.apply(App.scala:76)
at scala.collection.immutable.List.foreach(List.scala:381)
at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
at scala.App$class.main(App.scala:76)
at ScoptBug$.main(ScoptBug.scala:10)
at ScoptBug.main(ScoptBug.scala)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 44 throws an exception.
Line 50 throws an exception too, if you let it get there.