ENT-2848 Put some caching back into serialization transforms lookup. (#4397)

* Attempt to put some caching back into transforms lookup.

Cleanup for PR after testing.

* Match spacing in OS.

* Avoid checking two caches.
This commit is contained in:
Rick Parker 2018-12-11 13:47:06 +00:00 committed by GitHub
parent 4799df9b80
commit 85102fa0e5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 6 deletions

View File

@ -53,6 +53,13 @@ interface LocalSerializerFactory {
*/
fun getTypeInformation(type: Type): LocalTypeInformation
/**
* Obtain [LocalTypeInformation] for the [Type] that has the given name in the [ClassLoader] associated with this factory.
*
* @return null if the type with the given name does not exist in the [ClassLoader] for this factory.
*/
fun getTypeInformation(typeName: String): LocalTypeInformation?
/**
* Use the [FingerPrinter] to create a type descriptor for the given [type].
*/
@ -84,12 +91,24 @@ class DefaultLocalSerializerFactory(
}
private val serializersByType: MutableMap<TypeIdentifier, AMQPSerializer<Any>> = DefaultCacheProvider.createCache()
private val typesByName = DefaultCacheProvider.createCache<String, Optional<LocalTypeInformation>>()
override fun createDescriptor(typeInformation: LocalTypeInformation): Symbol =
Symbol.valueOf("$DESCRIPTOR_DOMAIN:${fingerPrinter.fingerprint(typeInformation)}")
override fun getTypeInformation(type: Type): LocalTypeInformation = typeModel.inspect(type)
override fun getTypeInformation(typeName: String): LocalTypeInformation? {
return typesByName.getOrPut(typeName) {
val localType = try {
Class.forName(typeName, false, classloader)
} catch (_: ClassNotFoundException) {
null
}
Optional.ofNullable(localType?.run { getTypeInformation(this) })
}.orElse(null)
}
override fun get(typeInformation: LocalTypeInformation): AMQPSerializer<Any> =
get(typeInformation.observedType, typeInformation)

View File

@ -257,12 +257,7 @@ data class TransformsSchema(val types: Map<String, EnumMap<TransformTypes, Mutab
*/
fun build(schema: Schema, sf: LocalSerializerFactory): TransformsSchema {
val transformsMap = schema.types.asSequence().mapNotNull { type ->
val localType = try {
sf.classloader.loadClass(type.name)
} catch (_: ClassNotFoundException) {
return@mapNotNull null
}
val localTypeInformation = sf.getTypeInformation(localType)
val localTypeInformation = sf.getTypeInformation(type.name)
if (localTypeInformation is LocalTypeInformation.AnEnum) {
localTypeInformation.transforms.source.let {
if (it.isEmpty()) null else type.name to it