Add fast serialization for records
This commit is contained in:
parent
ed85046953
commit
6ab00df42c
2 changed files with 19 additions and 1 deletions
|
|
@ -8,9 +8,10 @@
|
|||
(encryption :as encryption :refer (aes128-encryptor))])
|
||||
(:import [java.io DataInputStream DataOutputStream ByteArrayOutputStream
|
||||
ByteArrayInputStream]
|
||||
[java.lang.reflect Method]
|
||||
[clojure.lang Keyword BigInt Ratio PersistentQueue PersistentTreeMap
|
||||
PersistentTreeSet IPersistentList IPersistentVector IPersistentMap
|
||||
IPersistentSet IPersistentCollection]))
|
||||
IPersistentSet IPersistentCollection IRecord]))
|
||||
|
||||
;;;; Nippy 2.x+ header spec (4 bytes)
|
||||
(def ^:private ^:const head-version 1)
|
||||
|
|
@ -61,6 +62,8 @@
|
|||
|
||||
(def ^:const id-ratio (int 70))
|
||||
|
||||
(def ^:const id-record (int 80))
|
||||
|
||||
;;; DEPRECATED (old types will be supported only for thawing)
|
||||
(def ^:const id-old-reader (int 1)) ; as of 0.9.2, for +64k support
|
||||
(def ^:const id-old-string (int 11)) ; as of 0.9.2, for +64k support
|
||||
|
|
@ -137,6 +140,10 @@
|
|||
(str ns "/" (name x))
|
||||
(name x))))
|
||||
|
||||
(freezer IRecord id-record
|
||||
(write-utf8 s (.getName (class x)))
|
||||
(freeze-to-stream s (into {} x)))
|
||||
|
||||
(coll-freezer PersistentQueue id-queue)
|
||||
(coll-freezer PersistentTreeSet id-sorted-set)
|
||||
(kv-freezer PersistentTreeMap id-sorted-map)
|
||||
|
|
@ -261,6 +268,12 @@
|
|||
id-ratio (/ (bigint (read-biginteger s))
|
||||
(bigint (read-biginteger s)))
|
||||
|
||||
id-record
|
||||
(let [class ^Class (Class/forName (read-utf8 s))
|
||||
meth-sig (into-array Class [IPersistentMap])
|
||||
method ^Method (.getMethod class "create" meth-sig)]
|
||||
(.invoke method class (into-array Object [(thaw-from-stream s)])))
|
||||
|
||||
;;; DEPRECATED
|
||||
id-old-reader (read-string (.readUTF s))
|
||||
id-old-string (.readUTF s)
|
||||
|
|
|
|||
|
|
@ -30,6 +30,11 @@
|
|||
(thaw (org.iq80.snappy.Snappy/uncompress iq80-ba 0 (alength iq80-ba)))
|
||||
(thaw (org.iq80.snappy.Snappy/uncompress xerial-ba 0 (alength xerial-ba))))))
|
||||
|
||||
;;; Records
|
||||
(defrecord RecordType [data])
|
||||
(expect RecordType (thaw (freeze (RecordType. "Joe"))))
|
||||
(expect (RecordType. "Joe") (thaw (freeze (RecordType. "Joe"))))
|
||||
|
||||
;;; Custom types
|
||||
(defrecord MyType [data])
|
||||
(nippy/extend-freeze MyType 1 [x s] (.writeUTF s (:data x)))
|
||||
|
|
|
|||
Loading…
Reference in a new issue