support for fixed size ints (all), but only BE

This commit is contained in:
Sergey Chernov 2023-04-01 17:17:42 +01:00
parent 86eb5e9d7f
commit 641188e92b
5 changed files with 102 additions and 24 deletions

View File

@ -101,3 +101,26 @@ This __field annontation__ allows to store __integer fields__ of any size more c
## @FixedSize(size) ## @FixedSize(size)
Use it with fixed-size collections (like hashes, keys, etc) to not to keep collection size in the packed binary. It saves at least one byte. Use it with fixed-size collections (like hashes, keys, etc) to not to keep collection size in the packed binary. It saves at least one byte.
## @Fixed
Can be used with any integer type to store/restor it as is, fixed-size, big-endian:
- Short, UShort: 2 bytes
- Int, UInt: 4 bytes
- Long, ULong: 8 bytes
Note that without this modifier all integers are serialized into variable-length compressed format, see class [Smartint] from this library.
Example:
~~~kotlin
@Serializable
class Foo(
@Fixed
val eightBytesLongInt: Long
)
// so:
assertEquals("00 00 00 01 00 00 00 02", BipackEncoder.encode(Foo(0x100000002)).encodeToHex())
~~~

View File

@ -35,7 +35,9 @@ class BipackDecoder(
if (fixedNumber) input.readI32() if (fixedNumber) input.readI32()
else if (nextIsUnsigned) input.readNumber<UInt>().toInt() else input.readNumber() else if (nextIsUnsigned) input.readNumber<UInt>().toInt() else input.readNumber()
override fun decodeLong(): Long = if (nextIsUnsigned) input.readNumber<ULong>().toLong() else input.readNumber() override fun decodeLong(): Long =
if( fixedNumber ) input.readI64()
else if (nextIsUnsigned) input.readNumber<ULong>().toLong() else input.readNumber()
override fun decodeFloat(): Float = input.readFloat() override fun decodeFloat(): Float = input.readFloat()
override fun decodeDouble(): Double = input.readDouble() override fun decodeDouble(): Double = input.readDouble()
override fun decodeChar(): Char = Char(input.readNumber<UInt>().toInt()) override fun decodeChar(): Char = Char(input.readNumber<UInt>().toInt())

View File

@ -44,7 +44,10 @@ class BipackEncoder(val output: DataSink) : AbstractEncoder() {
else output.writeNumber(value) else output.writeNumber(value)
fun encodeUInt(value: UInt) = output.writeNumber(value) fun encodeUInt(value: UInt) = output.writeNumber(value)
override fun encodeLong(value: Long) = if (nextIsUnsigned) override fun encodeLong(value: Long) =
if (fixedNumber)
output.writeI64(value)
else if (nextIsUnsigned)
output.writeNumber(value.toULong()) output.writeNumber(value.toULong())
else else
output.writeNumber(value) output.writeNumber(value)

View File

@ -48,16 +48,47 @@ annotation class CrcProtected
annotation class Unsigned annotation class Unsigned
/** /**
* Use it with collection of fixed size, like hash digest, key bits and so on, by not storing collection * Fixed size collection of a given size. __Use it only with collections!__
* size. It effectively reduced packed size to at least one byte. depending on the actual *
* Use it with collection of fixed size, to read/write exact number of items (for example, bytes),
* like hash digest, key bits and so on. Does not stores/loades the collection
* size what reduces packed size to at least one byte. depending on the actual
* collection size. As for nowonly collection types (e.g. ByteArray, List<T>m etc) are supported. * collection size. As for nowonly collection types (e.g. ByteArray, List<T>m etc) are supported.
* Note that if the actual collection size differs from [size], [BipackEncoder] will throw * Note that if the actual collection size differs from [size], [BipackEncoder] will throw
* [WrongCollectionSize] while encoding it. * [WrongCollectionSize] while encoding it. For example:
*
* ~~~
* @Serializable
* class Foo(
* @FixedSize(32)
* thirtyTwoBytes: ByteArray
* )
*/ */
@SerialInfo @SerialInfo
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY) @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
annotation class FixedSize(val size: Int) annotation class FixedSize(val size: Int)
/**
* Fixed-size number, big-endian. Could be used only with field of following types:
*
* - Int, UInt: 4 bytes
* - Short, UShort: 2 bytes
* - Long, ULong: 8 bytes.
*
* It should not be used with Byte or UByte as their size is always 1 byte ;)
*
* Example:
* ~~~
* @Serializable
* class Foo(
* @Fixed
* val eightBytesLongInt: Long
* )
*
* // so:
* assertEquals("00 00 00 01 00 00 00 02", BipackEncoder.encode(Foo(0x100000002)).encodeToHex())
* ~~~
*/
@SerialInfo @SerialInfo
@Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY) @Target(AnnotationTarget.FIELD, AnnotationTarget.PROPERTY)
annotation class Fixed annotation class Fixed

View File

@ -58,7 +58,7 @@ class BipackEncoderTest {
@Test @Test
fun encodeSimple() { fun encodeSimple() {
val a = Foobar1N(1,2)//, "bum") val a = Foobar1N(1, 2)//, "bum")
println(BipackEncoder.encode(a).toDump()) println(BipackEncoder.encode(a).toDump())
assertEquals(2, BipackEncoder.encode(a).size) assertEquals(2, BipackEncoder.encode(a).size)
val b = BipackDecoder.decode<Foobar1N>(BipackEncoder.encode(a)) val b = BipackDecoder.decode<Foobar1N>(BipackEncoder.encode(a))
@ -153,11 +153,11 @@ class BipackEncoderTest {
@Serializable @Serializable
data class Types1( data class Types1(
val i: Int = 7, val i: Int = 7,
val f: Float = 1f/3f, val f: Float = 1f / 3f,
val d: Double = 1.0/3.0, val d: Double = 1.0 / 3.0,
val b: Boolean = true, val b: Boolean = true,
val s: String = "жёпа", val s: String = "жёпа",
val ba: ByteArray = byteArrayOf(1,2,3), val ba: ByteArray = byteArrayOf(1, 2, 3),
val ch: Char = 'Ы', val ch: Char = 'Ы',
) { ) {
override fun equals(other: Any?): Boolean { override fun equals(other: Any?): Boolean {
@ -209,7 +209,7 @@ class BipackEncoderTest {
// less significant part (it actually uses native JS number which is longer than Float) // less significant part (it actually uses native JS number which is longer than Float)
// So for the test purposes we need exact values (that packs into float exacly, like nitegers // So for the test purposes we need exact values (that packs into float exacly, like nitegers
// and some decimals with finite and short binary representation): // and some decimals with finite and short binary representation):
val t1 = Types1(f=0.5f) val t1 = Types1(f = 0.5f)
val d = BipackEncoder.encode(t1) val d = BipackEncoder.encode(t1)
println(d.toDump()) println(d.toDump())
val t2: Types1 = d.decodeFromBipack() val t2: Types1 = d.decodeFromBipack()
@ -223,14 +223,14 @@ class BipackEncoderTest {
@Serializable @Serializable
data class Fixa( data class Fixa(
@FixedSize(5) @FixedSize(5)
val x: ByteArray val x: ByteArray,
) )
@Test @Test
fun textFixed() { fun textFixed() {
val x = byteArrayOf(1,2,3,4,5) val x = byteArrayOf(1, 2, 3, 4, 5)
//@Fixed(32) //@Fixed(32)
val y = Fixa(byteArrayOf(1,2,3,4,5)) val y = Fixa(byteArrayOf(1, 2, 3, 4, 5))
val d1 = BipackEncoder.encode(x) val d1 = BipackEncoder.encode(x)
println(d1.toDump()) println(d1.toDump())
assertEquals(6, d1.size) assertEquals(6, d1.size)
@ -241,14 +241,14 @@ class BipackEncoderTest {
assertContentEquals(x, BipackDecoder.decode<Fixa>(d2).x) assertContentEquals(x, BipackDecoder.decode<Fixa>(d2).x)
} }
// @Test // @Test
@Test @Test
fun testMaps() { fun testMaps() {
val t1 = mapOf("foo bar" to "f1", "bar" to "b1", "bazz" to "b3")//f=17f/7f) val t1 = mapOf("foo bar" to "f1", "bar" to "b1", "bazz" to "b3")//f=17f/7f)
val d = BipackEncoder.encode(t1) val d = BipackEncoder.encode(t1)
println(d.toDump()) println(d.toDump())
println(t1) println(t1)
println(BipackDecoder.decode<Map<String,String>>(d)) println(BipackDecoder.decode<Map<String, String>>(d))
// println(d.decodeFromBipack<Map<String,String>>()) // println(d.decodeFromBipack<Map<String,String>>())
// assertEquals(t1, d.decodeFromBipack()) // assertEquals(t1, d.decodeFromBipack())
} }
@ -260,7 +260,7 @@ class BipackEncoderTest {
val d = BipackEncoder.encode(t1) val d = BipackEncoder.encode(t1)
println(d.toDump()) println(d.toDump())
println(t1) println(t1)
println(BipackDecoder.decode<Map<String,Foobar1N>>(d)) println(BipackDecoder.decode<Map<String, Foobar1N>>(d))
// println(d.decodeFromBipack<Map<String,String>>()) // println(d.decodeFromBipack<Map<String,String>>())
// assertEquals(t1, d.decodeFromBipack()) // assertEquals(t1, d.decodeFromBipack())
} }
@ -274,7 +274,7 @@ class BipackEncoderTest {
val d = BipackEncoder.encode(t1) val d = BipackEncoder.encode(t1)
println(d.toDump()) println(d.toDump())
println(t1) println(t1)
println(BipackDecoder.decode<Map<String,Foobar1>>(d)) println(BipackDecoder.decode<Map<String, Foobar1>>(d))
// println(d.decodeFromBipack<Map<String,String>>()) // println(d.decodeFromBipack<Map<String,String>>())
// assertEquals(t1, d.decodeFromBipack()) // assertEquals(t1, d.decodeFromBipack())
} }
@ -288,7 +288,7 @@ class BipackEncoderTest {
@Serializable @Serializable
// @Framed // @Framed
@CrcProtected @CrcProtected
data class Outer(val i1: Inner,val i2: Inner) data class Outer(val i1: Inner, val i2: Inner)
@Test @Test
fun testNestedInOuterProtected() { fun testNestedInOuterProtected() {
@ -307,19 +307,38 @@ class BipackEncoderTest {
@Serializable @Serializable
data class FI16(@Fixed val i: Short) data class FI16(@Fixed val i: Short)
@Serializable
data class FI64(@Fixed val i: Long)
@Serializable @Serializable
data class FU16(@Fixed val i: UShort) data class FU16(@Fixed val i: UShort)
@Serializable
class Foo(
@Fixed
val eightBytesLongInt: Long,
)
@Test @Test
fun testFixedInt() { fun testFixedInt() {
val a= FI32(127) val a = FI32(127)
println(BipackEncoder.encode(a).toDump()) println(BipackEncoder.encode(a).toDump())
assertContentEquals(byteArrayOf(0,0,0,0x7f), BipackEncoder.encode(a)) assertContentEquals(byteArrayOf(0, 0, 0, 0x7f), BipackEncoder.encode(a))
assertEquals("FF FF FF FF", BipackEncoder.encode(FI32(-1)).encodeToHex()) assertEquals("FF FF FF FF", BipackEncoder.encode(FI32(-1)).encodeToHex())
assertEquals(-1, BipackEncoder.encode(FI32(-1)).decodeFromBipack<FI32>().i) assertEquals(-1, BipackEncoder.encode(FI32(-1)).decodeFromBipack<FI32>().i)
assertEquals("FF FF FF FF", BipackEncoder.encode(FU32(0xFFFFFFFFu)).encodeToHex()) assertEquals("FF FF FF FF", BipackEncoder.encode(FU32(0xFFFFFFFFu)).encodeToHex())
assertEquals("FF 01 02 03", BipackEncoder.encode(FU32(0xFF010203u)).encodeToHex()) assertEquals("FF 01 02 03", BipackEncoder.encode(FU32(0xFF010203u)).encodeToHex())
assertEquals("FF 03", BipackEncoder.encode(FU16(0xFF03u)).encodeToHex()) assertEquals("FF 03", BipackEncoder.encode(FU16(0xFF03u)).encodeToHex())
assertEquals(0x7ffeu, BipackEncoder.encode(FU16(0x7ffeu)).decodeFromBipack<FU16>().i) assertEquals(0x7ffeu, BipackEncoder.encode(FU16(0x7ffeu)).decodeFromBipack<FU16>().i)
assertEquals(-1, BipackEncoder.encode(FI16(-1)).decodeFromBipack<FI16>().i)
assertEquals(0x7ffe, BipackEncoder.encode(FI16(0x7ffe)).decodeFromBipack<FI16>().i)
assertEquals("00 00 00 01 00 00 00 02", BipackEncoder.encode(FI64(0x100000002)).encodeToHex())
assertEquals(0x100000002, BipackEncoder.encode(FI64(0x100000002)).decodeFromBipack<FI64>().i)
// so:
assertEquals("00 00 00 01 00 00 00 02", BipackEncoder.encode(Foo(0x100000002)).encodeToHex())
} }
} }