Fix MutableByteArrayHandler placement and update readme info

Relocated MutableByteArrayHandler implementation to correct position in ByteArrayHandler file for better organization. Updated readme to clarify behavior when updating variable-length types like strings for improved compression efficiency.
This commit is contained in:
2024-12-13 22:00:00 +01:00
parent c64df75cd5
commit 69fa23a7a9
2 changed files with 47 additions and 46 deletions

View File

@@ -42,7 +42,7 @@ class Person : TypedByteArray(
```
The different between STRING and CLOB is the maximum size (32kb vs 2gb). For any type where the length isn't fixed we need to provide max size.
When for example a string is updated with a shorter string the remaining bytes are zeroed to make compression as efficient as possible.
When such a type is updated, for example we update a string with a shorter string the remaining bytes are zeroed to make compression as efficient as possible.
Strings are stored as UTF-8 bytes, there is also a CachedStringProperty which will keep a copy of the string so it isn't converted to a
native string every time it's accessed.

View File

@@ -1,50 +1,5 @@
package nl.astraeus.tba
class MutableByteArrayHandler(
buffer: SlicedByteArray
) : ByteArrayHandler(buffer) {
constructor(
buffer: ByteArray,
offset: Int = 0,
length: Int = buffer.size
) : this(
SlicedByteArray.wrap(buffer, offset, length)
)
operator fun set(index: Int, value: Byte) {
buffer[index] = value
}
fun setShort(index: Int, value: Short) = buffer.setShort(index, value)
fun setInt(index: Int, value: Int) = buffer.setInt(index, value)
fun setLong(index: Int, value: Long) = buffer.setLong(index, value)
fun setFloat(index: Int, value: Float) = buffer.setFloat(index, value)
fun setDouble(index: Int, value: Double) = buffer.setDouble(index, value)
fun setString(index: Int, value: String, maxLength: Int) {
val bytes = value.encodeToByteArray()
check(bytes.size + 2 <= maxLength) { "String is too long" }
buffer.setShort(index, bytes.size.toShort())
buffer.setBlob(index + 2, SlicedByteArray.wrap(bytes), maxLength - 2)
}
fun setClob(index: Int, value: String, maxLength: Int) {
val bytes = value.encodeToByteArray()
check(bytes.size + 4 <= maxLength) { "Clob is too long" }
buffer.setInt(index, bytes.size)
buffer.setBlob(index + 4, SlicedByteArray.wrap(bytes), maxLength - 4)
}
fun setBlob(index: Int, bytes: SlicedByteArray, maxLength: Int) {
check(bytes.size <= maxLength) { "Blob is too long" }
buffer.setBlob(index, bytes, maxLength)
}
}
open class ByteArrayHandler(
var buffer: SlicedByteArray
) {
@@ -103,3 +58,49 @@ open class ByteArrayHandler(
return MutableByteArrayHandler(buffer = SlicedByteArray(buffer.data, 0, buffer.data.size))
}
}
class MutableByteArrayHandler(
buffer: SlicedByteArray
) : ByteArrayHandler(buffer) {
constructor(
buffer: ByteArray,
offset: Int = 0,
length: Int = buffer.size
) : this(
SlicedByteArray.wrap(buffer, offset, length)
)
operator fun set(index: Int, value: Byte) {
buffer[index] = value
}
fun setShort(index: Int, value: Short) = buffer.setShort(index, value)
fun setInt(index: Int, value: Int) = buffer.setInt(index, value)
fun setLong(index: Int, value: Long) = buffer.setLong(index, value)
fun setFloat(index: Int, value: Float) = buffer.setFloat(index, value)
fun setDouble(index: Int, value: Double) = buffer.setDouble(index, value)
fun setString(index: Int, value: String, maxLength: Int) {
val bytes = value.encodeToByteArray()
check(bytes.size + 2 <= maxLength) { "String is too long" }
buffer.setShort(index, bytes.size.toShort())
buffer.setBlob(index + 2, SlicedByteArray.wrap(bytes), maxLength - 2)
}
fun setClob(index: Int, value: String, maxLength: Int) {
val bytes = value.encodeToByteArray()
check(bytes.size + 4 <= maxLength) { "Clob is too long" }
buffer.setInt(index, bytes.size)
buffer.setBlob(index + 4, SlicedByteArray.wrap(bytes), maxLength - 4)
}
fun setBlob(index: Int, bytes: SlicedByteArray, maxLength: Int) {
check(bytes.size <= maxLength) { "Blob is too long" }
buffer.setBlob(index, bytes, maxLength)
}
}