Skip to content

Commit

Permalink
move kryo to core
Browse files Browse the repository at this point in the history
  • Loading branch information
endink committed Jun 19, 2024
1 parent 2b107f8 commit 8577029
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 35 deletions.
1 change: 1 addition & 0 deletions core-starter/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-starter-jdbc")
testImplementation("org.springframework.boot:spring-boot-starter-data-jdbc")
testImplementation("org.springframework:spring-tx")
testImplementation("com.h2database:h2")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import com.labijie.caching.orm.TestEntity
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.EnableAutoConfiguration
import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.jdbc.core.JdbcTemplate
import org.springframework.test.context.ContextConfiguration
import org.springframework.test.context.junit.jupiter.SpringExtension
Expand All @@ -25,8 +25,8 @@ import kotlin.test.Test
* @author Anders Xiao
* @date 2019-03-25
*/
@EnableAutoConfiguration
@ExtendWith(SpringExtension::class)
//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
@DataJdbcTest
@Transactional(propagation = Propagation.NEVER)
@ContextConfiguration(classes = [CachingAutoConfiguration::class, TestConfiguration::class, TransactionalConfiguration::class])
Expand Down
1 change: 1 addition & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dependencies {
api("org.slf4j:slf4j-api")
compileOnly("com.esotericsoftware:kryo")
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.labijie.caching.redis.serialization.kryo
package com.labijie.caching.kryo

import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.KryoException
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @author Anders Xiao
* @date 2024-06-19
*/
package com.labijie.caching.kryo

import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.time.Duration


object DurationSerializer: Serializer<Duration>() {
override fun write(kryo: Kryo, output: Output, duration: Duration) {
output.writeLong(duration.toNanos())
}

override fun read(kryo: Kryo, input: Input, durationClass: Class<out Duration>): Duration {
val value = input.readLong()
return Duration.ofNanos(value)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @author Anders Xiao
* @date 2024-06-18
*/
package com.labijie.caching.redis.serialization.kryo
package com.labijie.caching.kryo


interface IKryoSerializer {
Expand Down
82 changes: 82 additions & 0 deletions core/src/main/kotlin/com/labijie/caching/kryo/KryoUtils.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/**
* @author Anders Xiao
* @date 2024-06-19
*/
package com.labijie.caching.kryo

import com.esotericsoftware.kryo.Kryo
import java.math.BigDecimal
import java.math.BigInteger
import java.net.URI
import java.net.URL
import java.nio.charset.Charset
import java.time.Duration
import java.time.Instant
import java.time.LocalDateTime
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentSkipListMap


object KryoUtils {
fun Kryo.registerBaseTypes() {
this.register(BigDecimal::class.java, 9)
this.register(BigInteger::class.java, 10)
this.register(BitSet::class.java, 11)
this.register(URI::class.java, URISerializer, 12)
this.register(UUID::class.java, UUIDSerializer, 13)
this.register(HashMap::class.java, 14)
this.register(ArrayList::class.java, 15)
this.register(LinkedList::class.java, 16)
this.register(HashSet::class.java, 17)
this.register(TreeSet::class.java, 18)
this.register(Hashtable::class.java, 19)
this.register(Date::class.java, DateSerializer, 20)
this.register(ConcurrentHashMap::class.java, 22)
this.register(Vector::class.java, 23)
this.register(StringBuffer::class.java, 24)

this.register(ByteArray::class.java, 25)
this.register(CharArray::class.java, 26)
this.register(IntArray::class.java, 27)
this.register(FloatArray::class.java, 28)
this.register(DoubleArray::class.java, 29)
this.register(BooleanArray::class.java, 29)
this.register(ShortArray::class.java, 31)
this.register(LongArray::class.java, 32)
this.register(Array<String>::class.java,33)
this.register(Array<Any>::class.java, 34)

this.register(LinkedHashMap::class.java, 40)
this.register(LinkedHashSet::class.java, 41)
this.register(Collections.EMPTY_LIST.javaClass, 42)
this.register(Collections.EMPTY_MAP.javaClass,43)
this.register(Collections.EMPTY_SET.javaClass,44)
this.register(listOf(null as Any?).javaClass.javaClass,45)
this.register(setOf(null as Any?).javaClass, 47)
this.register(setOf(null as Any?).javaClass, 48)
this.register(MutableCollection::class.java, 50)
this.register(ConcurrentSkipListMap::class.java, 51)
this.register(TreeMap::class.java, 52)
this.register(MutableMap::class.java, 53)
this.register(mutableListOf<Any>().javaClass, 54)

this.register(Collections.singletonMap(null as Any?, null as Any?).javaClass,56)
this.register(Collections.singletonList(null as Any?).javaClass,57)
this.register(Collections.singleton(null as Any?).javaClass,58)
this.register(Collections.unmodifiableSortedMap(sortedMapOf<String, Any?>()).javaClass,59)
this.register(Collections.unmodifiableCollection(listOf(null as Any?)).javaClass,60)
this.register(Collections.unmodifiableMap(mapOf<Any?, Any?>()).javaClass,61)
this.register(Collections.unmodifiableSet(setOf(null as Any?)).javaClass,62)
this.register(Collections.unmodifiableList(listOf(null as Any?)).javaClass,63)

this.register(TimeZone::class.java, 70)
this.register(Calendar::class.java, 71)
this.register(Locale::class.java, 72)
this.register(Charset::class.java, 73)
this.register(URL::class.java, 74)
this.register(Duration::class.java, DurationSerializer, 75)
this.register(LocalDateTime::class.java, 76)
this.register(Instant::class.java, 77)
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.labijie.caching.redis.serialization.kryo
package com.labijie.caching.kryo

import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.labijie.caching.redis.serialization.kryo
package com.labijie.caching.kryo

import java.util.UUID
import com.esotericsoftware.kryo.Kryo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.labijie.caching.redis.serialization.JacksonCacheDataSerializer
import com.labijie.caching.redis.serialization.JsonSmileDataSerializer
import com.labijie.caching.redis.serialization.KryoCacheDataSerializer
import com.labijie.caching.redis.serialization.KryoOptions
import com.labijie.caching.redis.serialization.kryo.IKryoSerializer
import com.labijie.caching.kryo.IKryoSerializer
import org.springframework.beans.factory.ObjectProvider
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.autoconfigure.AutoConfigureBefore
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
package com.labijie.caching.redis.serialization

import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.Serializer
import com.esotericsoftware.kryo.serializers.DefaultSerializers
import com.esotericsoftware.kryo.serializers.DefaultSerializers.*
import com.fasterxml.jackson.databind.type.TypeFactory
import com.labijie.caching.CacheException
import com.labijie.caching.kryo.*
import com.labijie.caching.kryo.DateSerializer
import com.labijie.caching.kryo.KryoUtils.registerBaseTypes
import com.labijie.caching.kryo.URISerializer
import com.labijie.caching.kryo.UUIDSerializer
import com.labijie.caching.redis.ICacheDataSerializer
import com.labijie.caching.redis.serialization.kryo.*
import com.labijie.caching.redis.serialization.kryo.PooledKryo
import java.lang.reflect.Type
import java.math.BigDecimal
import java.math.BigInteger
import java.net.URI
import java.net.URL
import java.nio.charset.Charset
import java.time.Duration
import java.time.Instant
import java.time.LocalDateTime
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentSkipListMap

/**
* Created with IntelliJ IDEA.
Expand Down Expand Up @@ -49,32 +58,9 @@ open class KryoCacheDataSerializer(private val kryoSerializer: IKryoSerializer?,
register(double.class, new DoubleSerializer());
*/
this.register(BigDecimal::class.java, DefaultSerializers.BigDecimalSerializer() as Serializer<*>, 9)
this.register(BigInteger::class.java, DefaultSerializers.BigIntegerSerializer(), 10)
this.register(BitSet::class.java, 11)
this.register(URI::class.java, URISerializer, 12)
this.register(UUID::class.java, UUIDSerializer, 13)
this.register(HashMap::class.java, 14)
this.register(ArrayList::class.java, 15)
this.register(LinkedList::class.java, 16)
this.register(HashSet::class.java, 17)
this.register(TreeSet::class.java, 18)
this.register(Hashtable::class.java, 19)
this.register(Date::class.java, DateSerializer, 20)
this.register(Calendar::class.java, 21)
this.register(ConcurrentHashMap::class.java, 22)
this.register(Vector::class.java, 23)
this.register(StringBuffer::class.java, 24)
this.register(ByteArray::class.java, 25)
this.register(CharArray::class.java, 26)
this.register(IntArray::class.java, 27)
this.register(FloatArray::class.java, 28)
this.register(DoubleArray::class.java, 29)
this.register(DoubleArray::class.java, 30)
this.register(ShortArray::class.java, 31)
registerBaseTypes()


this.register(LinkedHashMap::class.java, 40)
this.register(LinkedHashSet::class.java, 41)

kryoOptions.getRegistry().forEach {
if (it.id <= 100) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.labijie.caching.redis.serialization.kryo
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.*
import com.esotericsoftware.kryo.util.Pool
import com.labijie.caching.kryo.IKryoSerializer
import java.nio.ByteBuffer

/**
Expand Down

0 comments on commit 8577029

Please sign in to comment.