Simplify stream thaw API, switch from macros->fns

This commit is contained in:
Peter Taoussanis 2015-09-29 14:36:23 +07:00
parent 50ffb78c22
commit 15f0de1658

View file

@ -458,36 +458,41 @@
;;;; Thawing ;;;; Thawing
(declare thaw-from-in) (declare thaw-from-in!)
(defmacro read-bytes [in & [small?]] (defn read-bytes ^bytes [^DataInput in]
(let [rc (if small? 'readByte 'readInt)] (let [size (.readInt in)
`(let [in# ~in ba (byte-array size)]
size# (. in# ~rc) (.readFully in ba 0 size)
ba# (byte-array size#)] ba))
(.readFully in# ba# 0 size#)
ba#)))
(defmacro read-biginteger [in] `(BigInteger. (read-bytes ~in))) (defn read-sm-bytes ^bytes [^DataInput in]
(defmacro read-utf8 [in & [small?]] (let [size (.readByte in)
`(String. (read-bytes ~in ~small?) "UTF-8")) ba (byte-array size)]
(.readFully in ba 0 size)
ba))
(defmacro ^:private read-coll [in coll & [small?]] (defn read-biginteger ^BigInteger [^DataInput in] (BigInteger. (read-bytes in)))
(let [rc (if small? 'readByte 'readInt)] (defn read-utf8 ^String [^DataInput in] (String. (read-bytes in) "UTF-8"))
`(let [in# ~in] (defn read-sm-utf8 ^String [^DataInput in] (String. (read-sm-bytes in) "UTF-8"))
(enc/repeatedly-into ~coll (. in# ~rc)
(fn [] (thaw-from-in in#))))))
(defmacro ^:private read-kvs [in coll & [small?]] (defn read-coll [^DataInput in to-coll]
(let [rc (if small? 'readByte 'readInt)] (enc/repeatedly-into to-coll (.readInt in) (fn [] (thaw-from-in! in))))
`(let [in# ~in]
(enc/repeatedly-into ~coll (. in# ~rc)
(fn [] [(thaw-from-in in#) (thaw-from-in in#)])))))
(defmacro ^:private read-kvs-depr1 [in coll] (defn read-sm-coll [^DataInput in to-coll]
`(let [in# ~in] (enc/repeatedly-into to-coll (.readByte in) (fn [] (thaw-from-in! in))))
(enc/repeatedly-into ~coll (quot (.readInt in#) 2)
(fn [] [(thaw-from-in in#) (thaw-from-in in#)])))) (defn read-kvs [^DataInput in to-coll]
(enc/repeatedly-into to-coll (.readInt in)
(fn [] [(thaw-from-in! in) (thaw-from-in! in)])))
(defn read-sm-kvs [^DataInput in to-coll]
(enc/repeatedly-into to-coll (.readByte in)
(fn [] [(thaw-from-in! in) (thaw-from-in! in)])))
(defn read-kvs-depr1 [^DataInput in to-coll]
(enc/repeatedly-into to-coll (quot (.readInt in) 2)
(fn [] [(thaw-from-in! in) (thaw-from-in! in)])))
(def ^:private class-method-sig (into-array Class [IPersistentMap])) (def ^:private class-method-sig (into-array Class [IPersistentMap]))
@ -509,9 +514,12 @@
type-id) type-id)
{:internal-type-id type-id})))) {:internal-type-id type-id}))))
(defn- thaw-from-in (defn thaw-from-in!
[^DataInput in] "Deserializes a frozen object from given DataInput to its original Clojure
(let [type-id (.readByte in)] data type"
[^DataInput data-input]
(let [in data-input
type-id (.readByte in)]
(try (try
(when-debug-mode (when-debug-mode
(println (format "DEBUG - thawing type-id: %s" type-id))) (println (format "DEBUG - thawing type-id: %s" type-id)))
@ -544,8 +552,8 @@
:nippy/unthawable {:class-name class-name :content nil}}))) :nippy/unthawable {:class-name class-name :content nil}})))
id-record id-record
(let [class-name (read-utf8 in) (let [class-name (read-utf8 in)
content (thaw-from-in in)] content (thaw-from-in! in)]
(try (try
(let [class (Class/forName class-name) (let [class (Class/forName class-name)
method (.getMethod class "create" class-method-sig)] method (.getMethod class "create" class-method-sig)]
@ -564,26 +572,26 @@
id-keyword (keyword (read-utf8 in)) id-keyword (keyword (read-utf8 in))
;;; Optimized, common-case types (v2.6+) ;;; Optimized, common-case types (v2.6+)
id-sm-string (read-utf8 in :small) id-sm-string (read-sm-utf8 in)
id-sm-keyword (keyword (read-utf8 in :small)) id-sm-keyword (keyword (read-sm-utf8 in))
id-queue (read-coll in (PersistentQueue/EMPTY)) id-queue (read-coll in (PersistentQueue/EMPTY))
id-sorted-set (read-coll in (sorted-set)) id-sorted-set (read-coll in (sorted-set))
id-sorted-map (read-kvs in (sorted-map)) id-sorted-map (read-kvs in (sorted-map))
id-vector (read-coll in []) id-vector (read-coll in [])
id-sm-vector (read-coll in [] :small) id-sm-vector (read-sm-coll in [])
id-set (read-coll in #{}) id-set (read-coll in #{})
id-sm-set (read-coll in #{} :small) id-sm-set (read-sm-coll in #{})
id-map (read-kvs in {}) id-map (read-kvs in {})
id-sm-map (read-kvs in {} :small) id-sm-map (read-sm-kvs in {})
id-list (into '() (rseq (read-coll in []))) id-list (into '() (rseq (read-coll in [])))
id-seq (or (seq (read-coll in [])) id-seq (or (seq (read-coll in []))
(lazy-seq nil) ; Empty coll (lazy-seq nil) ; Empty coll
) )
id-meta (let [m (thaw-from-in in)] (with-meta (thaw-from-in in) m)) id-meta (let [m (thaw-from-in! in)] (with-meta (thaw-from-in! in) m))
id-byte (.readByte in) id-byte (.readByte in)
id-short (.readShort in) id-short (.readShort in)
@ -618,7 +626,7 @@
id-reader-depr1 (enc/read-edn (.readUTF in)) id-reader-depr1 (enc/read-edn (.readUTF in))
id-string-depr1 (.readUTF in) id-string-depr1 (.readUTF in)
id-map-depr1 (apply hash-map (enc/repeatedly-into [] (* 2 (.readInt in)) id-map-depr1 (apply hash-map (enc/repeatedly-into [] (* 2 (.readInt in))
(fn [] (thaw-from-in in)))) (fn [] (thaw-from-in! in))))
id-keyword-depr1 (keyword (.readUTF in)) id-keyword-depr1 (keyword (.readUTF in))
id-prefixed-custom ; Prefixed custom type id-prefixed-custom ; Prefixed custom type
@ -632,12 +640,6 @@
(throw (ex-info (format "Thaw failed against type-id: %s" type-id) (throw (ex-info (format "Thaw failed against type-id: %s" type-id)
{:type-id type-id} e)))))) {:type-id type-id} e))))))
(defn thaw-from-in!
"Low-level API. Deserializes a frozen object from given DataInput to its
original Clojure data type."
[data-input]
(thaw-from-in data-input))
(defn- try-parse-header [ba] (defn- try-parse-header [ba]
(when-let [[head-ba data-ba] (enc/ba-split ba 4)] (when-let [[head-ba data-ba] (enc/ba-split ba 4)]
(let [[head-sig* [meta-id]] (enc/ba-split head-ba 3)] (let [[head-sig* [meta-id]] (enc/ba-split head-ba 3)]