Housekeeping

This commit is contained in:
Peter Taoussanis 2013-06-16 11:50:36 +07:00
parent 63765c8510
commit 9c207cd6af

View file

@ -209,9 +209,7 @@
(defn coll-thaw (defn coll-thaw
"Thaws simple collection types." "Thaws simple collection types."
[coll ^DataInputStream s] [coll ^DataInputStream s]
(utils/repeatedly-into coll (utils/repeatedly-into coll (.readInt s) (thaw-from-stream s)))
(.readInt s)
(thaw-from-stream s)))
(defn coll-thaw-kvs (defn coll-thaw-kvs
"Thaws key-value collection types." "Thaws key-value collection types."
@ -225,7 +223,7 @@
(utils/case-eval (utils/case-eval
type-id type-id
id-reader (read-string (String. (read-bytes s) "UTF-8")) id-reader (read-string (read-utf8 s))
id-bytes (read-bytes s) id-bytes (read-bytes s)
id-nil nil id-nil nil
id-boolean (.readBoolean s) id-boolean (.readBoolean s)
@ -263,8 +261,7 @@
id-old-reader (read-string (.readUTF s)) id-old-reader (read-string (.readUTF s))
id-old-string (.readUTF s) id-old-string (.readUTF s)
id-old-map (apply hash-map (utils/repeatedly-into [] id-old-map (apply hash-map (utils/repeatedly-into []
(* 2 (.readInt s)) (* 2 (.readInt s)) (thaw-from-stream s)))
(thaw-from-stream s)))
id-old-keyword (keyword (.readUTF s)) id-old-keyword (keyword (.readUTF s))
(throw (Exception. (str "Failed to thaw unknown type ID: " type-id)))))) (throw (Exception. (str "Failed to thaw unknown type ID: " type-id))))))
@ -276,8 +273,7 @@
[data-ba (head-meta meta-id {:unrecognized-header? true})])))) [data-ba (head-meta meta-id {:unrecognized-header? true})]))))
(defn throw-thaw-exception [msg & [e]] (defn throw-thaw-ex [msg & [e]] (throw (Exception. (str "Thaw failed: " msg) e)))
(throw (Exception. (str "Thaw failed: " msg) e)))
(defn thaw (defn thaw
"Deserializes frozen bytes to their original Clojure data type. "Deserializes frozen bytes to their original Clojure data type.
@ -311,11 +307,13 @@
stream (DataInputStream. (ByteArrayInputStream. ba))] stream (DataInputStream. (ByteArrayInputStream. ba))]
(binding [*read-eval* read-eval?] (thaw-from-stream stream))) (binding [*read-eval* read-eval?] (thaw-from-stream stream)))
(catch Exception e (catch Exception e
(cond decrypt? (throw-thaw-exception "Wrong password/encryptor?" e) (cond
decompress? (throw-thaw-exception "Encrypted data or wrong compressor?" e) decrypt? (throw-thaw-ex "Wrong password/encryptor?" e)
:else (if apparent-header? decompress? (throw-thaw-ex "Encrypted data or wrong compressor?" e)
(throw-thaw-exception "Corrupt data?" e) :else
(throw-thaw-exception "Encrypted and/or compressed data?" e)))))))] (if apparent-header?
(throw-thaw-ex "Corrupt data?" e)
(throw-thaw-ex "Encrypted and/or compressed data?" e)))))))]
(if (= legacy-mode true) (if (= legacy-mode true)
(try-thaw-data ba nil) (try-thaw-data ba nil)
@ -330,23 +328,25 @@
(cond ; Trust metadata, give fancy error messages (cond ; Trust metadata, give fancy error messages
unrecognized-header? unrecognized-header?
(throw-thaw-exception "Unrecognized header. Data frozen with newer Nippy version?") (throw-thaw-ex
"Unrecognized header. Data frozen with newer Nippy version?")
(and strict? (not encrypted?) password) (and strict? (not encrypted?) password)
(throw-thaw-exception (str "Unencrypted data. Try again w/o password.\n" (throw-thaw-ex (str "Unencrypted data. Try again w/o password.\n"
"Disable `:strict?` option to ignore this error. ")) "Disable `:strict?` option to ignore this error. "))
(and strict? (not compressed?) compressor) (and strict? (not compressed?) compressor)
(throw-thaw-exception (str "Uncompressed data. Try again w/o compressor.\n" (throw-thaw-ex (str "Uncompressed data. Try again w/o compressor.\n"
"Disable `:strict?` option to ignore this error.")) "Disable `:strict?` option to ignore this error."))
(and compressed? (not compressor)) (and compressed? (not compressor))
(throw-thaw-exception "Compressed data. Try again with compressor.") (throw-thaw-ex "Compressed data. Try again with compressor.")
(and encrypted? (not password)) (and encrypted? (not password))
(throw-thaw-exception "Encrypted data. Try again with password.") (throw-thaw-ex "Encrypted data. Try again with password.")
:else (try-thaw-data data-ba head-meta))) :else (try-thaw-data data-ba head-meta)))
;; Header definitely not okay ;; Header definitely not okay
(if (= legacy-mode :auto) (if (= legacy-mode :auto)
(try-thaw-data ba nil) ; Legacy thaw (try-thaw-data ba nil) ; Legacy thaw
(throw-thaw-exception (str "Not Nippy data, data frozen with Nippy < 2.x, " (throw-thaw-ex
(str "Not Nippy data, data frozen with Nippy < 2.x, "
"or corrupt data?\n" "or corrupt data?\n"
"See `:legacy-mode` option for data frozen with Nippy < 2.x."))))))) "See `:legacy-mode` option for data frozen with Nippy < 2.x.")))))))