cache.AvroSchemaCache.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of avro-kotlin-serialization Show documentation
Show all versions of avro-kotlin-serialization Show documentation
Using kotlinx-serialization to encode/decode kotlin to avro.
The newest version!
package io.toolisticon.kotlin.avro.serialization.cache
import com.github.avrokotlin.avro4k.Avro
import com.github.avrokotlin.avro4k.schema
import io.toolisticon.kotlin.avro.model.wrapper.AvroSchema
import io.toolisticon.kotlin.avro.repository.AvroSchemaResolverMutableMap
import mu.KLogging
import org.apache.avro.util.WeakIdentityHashMap
import kotlin.reflect.KClass
class AvroSchemaCache(
private val avro4k: Avro,
private val kSerializerCache: KSerializerCache,
private val schemaResolver: AvroSchemaResolverMutableMap,
) : AvroCache.SchemaByClassCache {
companion object : KLogging()
private val store: WeakIdentityHashMap, AvroSchema> = WeakIdentityHashMap()
override operator fun get(klass: KClass<*>): AvroSchema = store.getOrPut(klass) {
logger.trace { "add schema for $klass." }
val serializer = kSerializerCache[klass]
AvroSchema(avro4k.schema(serializer)).also { schemaResolver + it }
}
fun keys(): Set> = store.keys.sortedBy { it.simpleName }.toSet()
override fun toString() = "AvroSchemaCache(keys=${keys()})"
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy