Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
(defn write-record
"Write an associative data structure to Kryo's buffer. Write record
name as a string, count as an int, then serialize the key/value pairs."
[^Kryo registry ^Output output r]
(.writeString output (.getName (class r)) true)
(.writeInt output (count r) true)
(doseq [[k v] r]
(.writeClassAndObject registry output k)
(.writeClassAndObject registry output v)))
(defn read-record
"Read a record from Kryo's buffer. Read record name, then count, then deserialize
alternating key/value pairs. Transients are used for performance."
[^Kryo registry ^Input input]
(let [name (last (s/split (.readString input true) #"\."))]
(loop [remaining (.readInt input true)
data (transient {})]
(if (zero? remaining)
(eval `(~(symbol (str "map->" name)) ~(persistent! data)))
(recur (dec remaining)
(assoc! data
(.readClassAndObject registry input)
(.readClassAndObject registry input))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment