From 104e3b586578f129579d50a0cb208e058da9eda9 Mon Sep 17 00:00:00 2001 From: Chuckame Date: Tue, 16 Apr 2024 01:38:43 +0200 Subject: [PATCH] remove namingStrategy cache as there is a schema cache --- .../com/github/avrokotlin/avro4k/Avro.kt | 9 +-- .../avrokotlin/avro4k/AvroConfiguration.kt | 58 +------------------ .../avrokotlin/avro4k/decoder/ListDecoder.kt | 4 +- .../avrokotlin/avro4k/decoder/MapDecoder.kt | 4 +- .../avro4k/decoder/RecordDecoder.kt | 4 +- .../avro4k/decoder/RootRecordDecoder.kt | 4 +- .../avrokotlin/avro4k/decoder/UnionDecoder.kt | 4 +- .../avrokotlin/avro4k/encoder/ListEncoder.kt | 4 +- .../avrokotlin/avro4k/encoder/MapEncoder.kt | 4 +- .../avro4k/encoder/RecordEncoder.kt | 6 +- .../avro4k/encoder/RootRecordEncoder.kt | 4 +- .../avrokotlin/avro4k/encoder/UnionEncoder.kt | 4 +- .../avro4k/AvroSerializationAssertThat.kt | 4 +- 13 files changed, 26 insertions(+), 87 deletions(-) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt index 40fad35b..53a9af85 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt @@ -158,16 +158,11 @@ class AvroOutputStreamBuilder( } class Avro internal constructor( - internal val configuration: AvroInternalConfiguration, - override val serializersModule: SerializersModule, + override val serializersModule: SerializersModule = defaultModule, + internal val configuration: AvroConfiguration = AvroConfiguration(), ) : SerialFormat, BinaryFormat { internal val schemaCache: MutableMap = ConcurrentHashMap() - constructor( - serializersModule: SerializersModule = defaultModule, - configuration: AvroConfiguration = AvroConfiguration(), - ) : this(AvroInternalConfiguration(configuration), serializersModule) - constructor(configuration: AvroConfiguration) : this(defaultModule, configuration) companion object { diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt index ffaaa984..5d9ffbda 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/AvroConfiguration.kt @@ -1,10 +1,7 @@ package com.github.avrokotlin.avro4k import com.github.avrokotlin.avro4k.schema.FieldNamingStrategy -import com.github.avrokotlin.avro4k.schema.RecordName import com.github.avrokotlin.avro4k.schema.RecordNamingStrategy -import kotlinx.serialization.descriptors.SerialDescriptor -import java.util.concurrent.ConcurrentHashMap data class AvroConfiguration( /** @@ -24,57 +21,4 @@ data class AvroConfiguration( * When set to `true`, the nullable fields that haven't any default value are set as null if the value is missing. It also adds `"default": null` to those fields when generating schema using avro4k. */ val implicitNulls: Boolean = false, - /** - * Enable caching of resolved names. - * - * Default: `true` - */ - val namingCacheEnabled: Boolean = true, -) - -class AvroInternalConfiguration private constructor( - val recordNamingStrategy: RecordNamingStrategy, - val fieldNamingStrategy: FieldNamingStrategy, - val implicitNulls: Boolean, -) { - constructor(configuration: AvroConfiguration) : this( - recordNamingStrategy = configuration.recordNamingStrategy.cachedIfNecessary(configuration.namingCacheEnabled), - fieldNamingStrategy = configuration.fieldNamingStrategy.cachedIfNecessary(configuration.namingCacheEnabled), - implicitNulls = configuration.implicitNulls - ) -} - -internal fun RecordNamingStrategy.cachedIfNecessary(cacheEnabled: Boolean): RecordNamingStrategy = - if (!cacheEnabled) { - this - } else { - object : RecordNamingStrategy { - private val cache = ConcurrentHashMap() - - override fun resolve( - descriptor: SerialDescriptor, - serialName: String, - ): RecordName = - cache.getOrPut(descriptor) { - this@cachedIfNecessary.resolve(descriptor, serialName) - } - } - } - -internal fun FieldNamingStrategy.cachedIfNecessary(cacheEnabled: Boolean): FieldNamingStrategy = - if (!cacheEnabled) { - this - } else { - object : FieldNamingStrategy { - private val cache = ConcurrentHashMap, String>() - - override fun resolve( - descriptor: SerialDescriptor, - elementIndex: Int, - serialName: String, - ): String = - cache.getOrPut(descriptor to elementIndex) { - this@cachedIfNecessary.resolve(descriptor, elementIndex, serialName) - } - } - } \ No newline at end of file +) \ No newline at end of file diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt index 1fd60d69..c25d640e 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/ListDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import kotlinx.serialization.DeserializationStrategy import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.descriptors.PolymorphicKind @@ -19,7 +19,7 @@ class ListDecoder( private val schema: Schema, private val array: List, override val serializersModule: SerializersModule, - private val configuration: AvroInternalConfiguration, + private val configuration: AvroConfiguration, ) : AbstractDecoder(), FieldDecoder { init { require(schema.type == Schema.Type.ARRAY) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt index 4f6e7202..37f1b271 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/MapDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException import kotlinx.serialization.descriptors.PolymorphicKind @@ -20,7 +20,7 @@ class MapDecoder( private val schema: Schema, map: Map<*, *>, override val serializersModule: SerializersModule, - private val configuration: AvroInternalConfiguration, + private val configuration: AvroConfiguration, ) : AbstractDecoder(), CompositeDecoder { init { require(schema.type == Schema.Type.MAP) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt index 840ad685..2e74a857 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RecordDecoder.kt @@ -1,7 +1,7 @@ package com.github.avrokotlin.avro4k.decoder import com.github.avrokotlin.avro4k.AnnotationExtractor -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import com.github.avrokotlin.avro4k.schema.extractNonNull import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException @@ -31,7 +31,7 @@ class RecordDecoder( private val desc: SerialDescriptor, private val record: GenericRecord, override val serializersModule: SerializersModule, - private val configuration: AvroInternalConfiguration, + private val configuration: AvroConfiguration, ) : AbstractDecoder(), FieldDecoder { private var currentIndex = -1 diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt index a14cce62..9c53b9c4 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/RootRecordDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException import kotlinx.serialization.descriptors.PolymorphicKind @@ -16,7 +16,7 @@ import org.apache.avro.generic.GenericRecord class RootRecordDecoder( private val record: GenericRecord, override val serializersModule: SerializersModule, - private val configuration: AvroInternalConfiguration, + private val configuration: AvroConfiguration, ) : AbstractDecoder() { var decoded = false diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt index 2253ef96..a8bdcb7d 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/decoder/UnionDecoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.decoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import com.github.avrokotlin.avro4k.possibleSerializationSubclasses import com.github.avrokotlin.avro4k.schema.RecordName import kotlinx.serialization.DeserializationStrategy @@ -18,7 +18,7 @@ class UnionDecoder( descriptor: SerialDescriptor, private val value: GenericRecord, override val serializersModule: SerializersModule, - private val configuration: AvroInternalConfiguration, + private val configuration: AvroConfiguration, ) : AbstractDecoder(), FieldDecoder { private enum class DecoderState(val index: Int) { BEFORE(0), diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt index ef2a9b8d..3cac4e51 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/ListEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.descriptors.SerialDescriptor import kotlinx.serialization.encoding.AbstractEncoder @@ -15,7 +15,7 @@ import java.nio.ByteBuffer class ListEncoder( private val schema: Schema, override val serializersModule: SerializersModule, - override val configuration: AvroInternalConfiguration, + override val configuration: AvroConfiguration, private val callback: (GenericData.Array) -> Unit, ) : AbstractEncoder(), StructureEncoder { private val list = mutableListOf() diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt index 76ec1743..c6599179 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/MapEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException import kotlinx.serialization.descriptors.SerialDescriptor @@ -16,7 +16,7 @@ import java.nio.ByteBuffer class MapEncoder( schema: Schema, override val serializersModule: SerializersModule, - override val configuration: AvroInternalConfiguration, + override val configuration: AvroConfiguration, private val callback: (Map) -> Unit, ) : AbstractEncoder(), CompositeEncoder, diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt index 4ca89285..df763b6a 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RecordEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import com.github.avrokotlin.avro4k.ListRecord import com.github.avrokotlin.avro4k.Record import com.github.avrokotlin.avro4k.schema.extractNonNull @@ -19,7 +19,7 @@ import java.nio.ByteBuffer @ExperimentalSerializationApi interface StructureEncoder : FieldEncoder { - val configuration: AvroInternalConfiguration + val configuration: AvroConfiguration override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder { return when (descriptor.kind) { @@ -45,7 +45,7 @@ internal val SerialDescriptor.unwrapValueClass: SerialDescriptor class RecordEncoder( private val schema: Schema, override val serializersModule: SerializersModule, - override val configuration: AvroInternalConfiguration, + override val configuration: AvroConfiguration, val callback: (Record) -> Unit, ) : AbstractEncoder(), StructureEncoder { private val builder = RecordBuilder(schema) diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt index 5bacb035..05600355 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/RootRecordEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import com.github.avrokotlin.avro4k.Record import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.SerializationException @@ -16,7 +16,7 @@ import org.apache.avro.Schema class RootRecordEncoder( private val schema: Schema, override val serializersModule: SerializersModule, - private val configuration: AvroInternalConfiguration, + private val configuration: AvroConfiguration, private val callback: (Record) -> Unit, ) : AbstractEncoder() { override fun beginStructure(descriptor: SerialDescriptor): CompositeEncoder { diff --git a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt index 2e5908fd..4397451e 100644 --- a/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt +++ b/src/main/kotlin/com/github/avrokotlin/avro4k/encoder/UnionEncoder.kt @@ -1,6 +1,6 @@ package com.github.avrokotlin.avro4k.encoder -import com.github.avrokotlin.avro4k.AvroInternalConfiguration +import com.github.avrokotlin.avro4k.AvroConfiguration import com.github.avrokotlin.avro4k.Record import com.github.avrokotlin.avro4k.schema.RecordName import kotlinx.serialization.ExperimentalSerializationApi @@ -16,7 +16,7 @@ import org.apache.avro.Schema class UnionEncoder( private val unionSchema: Schema, override val serializersModule: SerializersModule, - private val configuration: AvroInternalConfiguration, + private val configuration: AvroConfiguration, private val callback: (Record) -> Unit, ) : AbstractEncoder() { override fun encodeString(value: String) { diff --git a/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt b/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt index 6f0f78dc..911b9ab5 100644 --- a/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt +++ b/src/test/kotlin/com/github/avrokotlin/avro4k/AvroSerializationAssertThat.kt @@ -24,7 +24,7 @@ import java.nio.file.Path class AvroSerializationAssertThat(private val valueToEncode: T, private val serializer: KSerializer) { private var serializersModule: SerializersModule = Avro.default.serializersModule - private var config: AvroInternalConfiguration = Avro.default.configuration + private var config: AvroConfiguration = Avro.default.configuration private var readerSchema: Schema = avro.schema(serializer) private lateinit var writerSchema: Schema private val avro: Avro @@ -36,7 +36,7 @@ class AvroSerializationAssertThat(private val valueToEncode: T, private val s } fun withConfig(config: AvroConfiguration): AvroSerializationAssertThat { - this.config = AvroInternalConfiguration(config) + this.config = config return this }