MapSchema

class MapSchema<K, V>(val keySchema: FateSchema<K>, val valueSchema: FateSchema<V>) : FateSchema<@JvmSuppressWildcards Map<K, V>>

A schema for maps with independently typed keys and values.

Keys can be any type that has a schema, so Map<Int, Pose> and Map<MyEnum, Double> both work.

Binary format

The schema:

[4 bytes: tag] [key schema blob] [value schema blob]

An encoded map:

[4 bytes: entry count N] [key1][value1] [key2][value2] ... [keyN][valueN]

Entries are written in the map's own iteration order, and the format does not depend on it. Every key is written, so a HashMap and a LinkedHashMap with the same entries both decode correctly, but their bytes differ.

Mutation

gay.zharel.fateweaver.log.FateLogWriter.write calls objSize and encodeObject separately, so don't mutate the map in between. If you do, the write fails with a java.nio.BufferOverflowException or the writer's "encoded object does not match reported size" check. It does not write corrupt bytes. Pass a copy if another thread might touch the map.

Nulls

A null key or value needs an OptionalSchema on that side. Map<String, Double?> infers as MapSchema(StringSchema, OptionalSchema(DoubleSchema)), and each absent value costs one byte. A null passed to a schema that is not optional throws.

Size limits

objSize returns an Int. A map that encodes to more than 2 GiB overflows it, and the writer's buffer allocation then throws without saying why.

Example

val powers = writer.createChannel("Motors/Powers", MapSchema(StringSchema, DoubleSchema))
powers.put(mapOf("fl" to 0.5, "fr" to -0.5))

Parameters

K

the key type

V

the value type

keySchema

schema used to encode each key

valueSchema

schema used to encode each value

Constructors

Link copied to clipboard
constructor(keySchema: FateSchema<K>, valueSchema: FateSchema<V>)

Properties

Link copied to clipboard
Link copied to clipboard
open override val schema: ByteArray

The schema definition as bytes, which the writer puts in the log.

Link copied to clipboard
open override val schemaSize: Int

Number of bytes in this schema's definition.

Link copied to clipboard
open override val tag: Int

The integer that identifies this schema's type in the log.

Link copied to clipboard

Functions

Link copied to clipboard
open override fun encodeObject(buffer: ByteBuffer, obj: Map<K, V>)

Writes the given object into the buffer.

Link copied to clipboard
open fun encodeSchema(buffer: ByteBuffer)

Writes the whole schema definition, starting with the tag, into the buffer.

Link copied to clipboard
open override fun objSize(obj: Map<K, V>): Int

Returns the number of bytes encodeObject writes for the given object.