From 94784e0addc7c3028fcf177b6395e0c3045f5c17 Mon Sep 17 00:00:00 2001 From: Valentyn Sobol Date: Wed, 18 Sep 2024 14:08:27 +0300 Subject: [PATCH] Fix dependencies and ERS bindings (#268) [jacodb-core] Add DoubleBinging DoubleBinging is another BuiltInBinding allowing to set Entities' properties & blobs of type Double. [jacodb-core] Make foreign dependencies for implementations of PluggableKeyValueStorage optional Set Libs.xodusEnvironment, Libs.lmdb_java & Libs.rocks_db dependencies as compileOnly. --- jacodb-core/build.gradle.kts | 11 ++++++---- .../impl/storage/ers/BuiltInBindings.kt | 12 +++++++++++ .../testing/storage/ers/BindingsTest.kt | 20 +++++++++++++++++++ 3 files changed, 39 insertions(+), 4 deletions(-) diff --git a/jacodb-core/build.gradle.kts b/jacodb-core/build.gradle.kts index 487a9592e..29f0a064f 100644 --- a/jacodb-core/build.gradle.kts +++ b/jacodb-core/build.gradle.kts @@ -56,17 +56,20 @@ dependencies { implementation(Libs.jdot) implementation(Libs.guava) implementation(Libs.sqlite) - implementation(Libs.xodusUtils) - implementation(Libs.xodusEnvironment) implementation(Libs.hikaricp) - implementation(Libs.lmdb_java) - implementation(Libs.rocks_db) + implementation(Libs.xodusUtils) + compileOnly(Libs.xodusEnvironment) + compileOnly(Libs.lmdb_java) + compileOnly(Libs.rocks_db) testImplementation(Libs.javax_activation) testImplementation(Libs.javax_mail) testImplementation(Libs.joda_time) testImplementation(Libs.slf4j_simple) testImplementation(Libs.hikaricp) + testImplementation(Libs.xodusEnvironment) + testImplementation(Libs.lmdb_java) + testImplementation(Libs.rocks_db) testFixturesImplementation(project(":jacodb-api-jvm")) testFixturesImplementation(kotlin("reflect")) diff --git a/jacodb-core/src/main/kotlin/org/jacodb/impl/storage/ers/BuiltInBindings.kt b/jacodb-core/src/main/kotlin/org/jacodb/impl/storage/ers/BuiltInBindings.kt index bbaa69f19..5357e7243 100644 --- a/jacodb-core/src/main/kotlin/org/jacodb/impl/storage/ers/BuiltInBindings.kt +++ b/jacodb-core/src/main/kotlin/org/jacodb/impl/storage/ers/BuiltInBindings.kt @@ -39,6 +39,7 @@ private val builtInBindings: Array> = arrayOf( StringBinding, // UTF-8 strings IntegerBinding, // 4-byte signed integers LongBinding, // 8-byte signed integers (longs) + DoubleBinging, // 8-byte floating point numbers (doubles) BooleanBinding // boolean values ) @@ -240,6 +241,17 @@ private object LongBinding : BuiltInBinding() { } } +private object DoubleBinging : BuiltInBinding() { + + override val classes: Set> get() = setOf(Double::class.java, java.lang.Double::class.java) + + override fun getBytes(obj: Double): ByteArray = LongBinding.getBytes(java.lang.Double.doubleToLongBits(obj)) + + override fun getObject(bytes: ByteArray, offset: Int): Double = + java.lang.Double.longBitsToDouble(LongBinding.getObject(bytes, offset)) + +} + private object BooleanBinding : BuiltInBinding() { override val classes = setOf(Boolean::class.java, java.lang.Boolean::class.java) diff --git a/jacodb-core/src/test/kotlin/org/jacodb/testing/storage/ers/BindingsTest.kt b/jacodb-core/src/test/kotlin/org/jacodb/testing/storage/ers/BindingsTest.kt index 442cd6f43..7abd4196f 100644 --- a/jacodb-core/src/test/kotlin/org/jacodb/testing/storage/ers/BindingsTest.kt +++ b/jacodb-core/src/test/kotlin/org/jacodb/testing/storage/ers/BindingsTest.kt @@ -72,6 +72,26 @@ class BindingsTest { assertEquals(Long.MAX_VALUE shr 17, serializeDeserializeCompressed(Long.MAX_VALUE shr 17)) } + @Test + fun booleanBinding() { + assertEquals(false, serializeDeserialize(false)) + assertEquals(true, serializeDeserialize(true)) + } + + @Test + fun doubleBinding() { + assertEquals(.0, serializeDeserialize(.0)) + assertEquals(1.0, serializeDeserialize(1.0)) + assertEquals(2.71281828, serializeDeserialize(2.71281828)) + assertEquals(3.14159265259, serializeDeserialize(3.14159265259)) + assertEquals(Double.NEGATIVE_INFINITY, serializeDeserialize(Double.NEGATIVE_INFINITY)) + assertEquals(Double.POSITIVE_INFINITY, serializeDeserialize(Double.POSITIVE_INFINITY)) + assertEquals(Double.MIN_VALUE, serializeDeserialize(Double.MIN_VALUE)) + assertEquals(Double.MAX_VALUE, serializeDeserialize(Double.MAX_VALUE)) + assertEquals(java.lang.Double.MIN_NORMAL, serializeDeserialize(java.lang.Double.MIN_NORMAL)) + assertEquals(Double.NaN, serializeDeserialize(Double.NaN)) + } + private companion object { val hash = byteArrayOf( -17, -65, -67, 68, 85, 96, -17, -65, -67, 28, 4, 76, 28,