All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cache.AvroSchemaCache.kt Maven / Gradle / Ivy

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