Merge pull request #26 from weavejester/fast-records

Fast serialization for records
This commit is contained in:
Peter Taoussanis 2013-08-06 23:54:33 -07:00
commit e48ccb4c45
2 changed files with 19 additions and 1 deletions

View file

@ -10,9 +10,10 @@
(edn :as edn)])
(: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)
@ -63,6 +64,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
@ -139,6 +142,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)
@ -263,6 +270,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 (edn/read-string (.readUTF s))
id-old-string (.readUTF s)

View file

@ -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)))