Skip to content

Commit

Permalink
remove namingStrategy cache as there is a schema cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Chuckame committed Apr 22, 2024
1 parent 29cc5e1 commit 69a369a
Show file tree
Hide file tree
Showing 15 changed files with 29 additions and 92 deletions.
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ dependencies {

tasks.withType<KotlinCompile>().configureEach {
kotlinOptions.jvmTarget = "1.8"
kotlinOptions.apiVersion = "1.8"
kotlinOptions.languageVersion = "1.8"
kotlinOptions.freeCompilerArgs += listOf("-opt-in=kotlinx.serialization.ExperimentalSerializationApi", "-Xcontext-receivers")
kotlinOptions.apiVersion = "1.6"
kotlinOptions.languageVersion = "1.6"
kotlinOptions.freeCompilerArgs += listOf("-opt-in=kotlinx.serialization.ExperimentalSerializationApi", "-opt-in=kotlin.RequiresOptIn", "-Xcontext-receivers")
}
java {
sourceCompatibility = JavaVersion.VERSION_1_8
Expand Down
9 changes: 2 additions & 7 deletions src/main/kotlin/com/github/avrokotlin/avro4k/Avro.kt
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,11 @@ class AvroOutputStreamBuilder<T>(
}

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<SerialDescriptor, Schema> = ConcurrentHashMap()

constructor(
serializersModule: SerializersModule = defaultModule,
configuration: AvroConfiguration = AvroConfiguration(),
) : this(AvroInternalConfiguration(configuration), serializersModule)

constructor(configuration: AvroConfiguration) : this(defaultModule, configuration)

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
/**
Expand All @@ -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<SerialDescriptor, RecordName>()

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<Pair<SerialDescriptor, Int>, String>()

override fun resolve(
descriptor: SerialDescriptor,
elementIndex: Int,
serialName: String,
): String =
cache.getOrPut(descriptor to elementIndex) {
this@cachedIfNecessary.resolve(descriptor, elementIndex, serialName)
}
}
}
)
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -19,7 +19,7 @@ class ListDecoder(
private val schema: Schema,
private val array: List<Any?>,
override val serializersModule: SerializersModule,
private val configuration: AvroInternalConfiguration,
private val configuration: AvroConfiguration,
) : AbstractDecoder(), FieldDecoder {
init {
require(schema.type == Schema.Type.ARRAY)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Any?>) -> Unit,
) : AbstractEncoder(), StructureEncoder {
private val list = mutableListOf<Any?>()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Utf8, *>) -> Unit,
) : AbstractEncoder(),
CompositeEncoder,
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.github.avrokotlin.avro4k.schema

import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.descriptors.SerialDescriptor
import org.apache.avro.Schema

internal class InlineClassVisitor(
override val context: VisitorContext,
private val onSchemaBuilt: (Schema) -> Unit,
) : SerialDescriptorInlineClassVisitor, AvroVisitorContextAware {
@OptIn(InternalSerializationApi::class)
override fun visitInlineClassElement(
inlineClassDescriptor: SerialDescriptor,
inlineElementIndex: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import java.nio.file.Path

class AvroSerializationAssertThat<T>(private val valueToEncode: T, private val serializer: KSerializer<T>) {
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
Expand All @@ -36,7 +36,7 @@ class AvroSerializationAssertThat<T>(private val valueToEncode: T, private val s
}

fun withConfig(config: AvroConfiguration): AvroSerializationAssertThat<T> {
this.config = AvroInternalConfiguration(config)
this.config = config
return this
}

Expand Down

0 comments on commit 69a369a

Please sign in to comment.