Skip to content

Commit

Permalink
feat: SingleObjectEncodedBytes as simple class
Browse files Browse the repository at this point in the history
  • Loading branch information
jangalinski committed Aug 10, 2024
1 parent c54482d commit 82951b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 27 deletions.
43 changes: 17 additions & 26 deletions avro-kotlin/src/main/kotlin/value/SingleObjectEncodedBytes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import java.nio.ByteBuffer
/**
* Message encoded as [Single Object](https://avro.apache.org/docs/current/spec.html#single_object_encoding) ByteArray.
*/
@JvmInline
value class SingleObjectEncodedBytes private constructor(
private val pair: Pair<AvroFingerprint, BinaryEncodedBytes>
class SingleObjectEncodedBytes private constructor(
/**
* The complete single object encoded bytes including marker bytes, fingerprint and payload.
*/
override val value: ByteArray
) : ByteArrayValueType {

companion object {
Expand All @@ -24,7 +26,7 @@ value class SingleObjectEncodedBytes private constructor(
val AVRO_HEADER_LENGTH = AvroHeaderBytes.size + Long.SIZE_BYTES

/**
* @param the byteArray to verify
* @param The byteArray to verify.
* @return the same byteArray for fluent usage
* @throws BadHeaderException if byteArray does to start with V1_HEADER or is too short to contain a fingerprint.
*/
Expand All @@ -35,10 +37,7 @@ value class SingleObjectEncodedBytes private constructor(
}
})

fun of(bytes: ByteArrayValue) = SingleObjectEncodedBytes(
fingerprint = AvroFingerprint.of(bytes),
payload = BinaryEncodedBytes(bytes.value.copyOfRange(AVRO_HEADER_LENGTH, bytes.value.size))
)
fun of(bytes: ByteArrayValue) = SingleObjectEncodedBytes(bytes.value).also { requireNotNull(it.fingerprint) }

fun of(bytes: ByteBuffer) = of(ByteArrayValue(bytes))

Expand All @@ -47,31 +46,23 @@ value class SingleObjectEncodedBytes private constructor(
fun of(bytes: ByteArray) = of(ByteArrayValue(bytes))
}

constructor(fingerprint: AvroFingerprint, payload: BinaryEncodedBytes) : this(fingerprint to payload)

override val value: ByteArray get() = AvroHeaderBytes.value + fingerprint.byteValue.value + payload.value

override val hex: HexString
get() = super.hex
override val buffer: ByteBuffer
get() = super.buffer
override val size: Int
get() = super.size

override fun get(index: Int): Byte {
return super.get(index)
constructor(fingerprint: AvroFingerprint, payload: BinaryEncodedBytes) : this(value = AvroHeaderBytes.value + fingerprint.byteValue.value + payload.value) {
require(this.fingerprint == fingerprint)
}

override val hex: HexString by lazy { HexString.of(value) }

/**
* The [AvroFingerprint] of this encoded byte array.
* Memoized for access.
* @return The schema fingerprint (as included in value(2,10)).
*/
val fingerprint: AvroFingerprint get() = pair.first
val fingerprint: AvroFingerprint by lazy { AvroFingerprint.of(bytes = ByteArrayValue(value)) }

/**
* The payload of this encoded byte array.
* The message payload, binary encoded (value(11,end)).
* @return copy of relevant bytes
*/
val payload: BinaryEncodedBytes get() = pair.second

val payload: BinaryEncodedBytes get() = BinaryEncodedBytes(value.copyOfRange(AVRO_HEADER_LENGTH, value.size))

override fun toString() = hex.formatted
}
2 changes: 1 addition & 1 deletion avro-kotlin/src/main/kotlin/value/_value-ktx.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ interface ByteArrayValueType : ValueType<ByteArray>, WithHexString {
*/
val size: Int get() = value.size

operator fun get(index: Int) = value[index]
operator fun get(index: Int): Byte = value[index]
}


Expand Down

0 comments on commit 82951b3

Please sign in to comment.