BREAKING (minor): Change freeze-to-bytes, thaw-from-bytes API when not using defaults.
Breaks only for users that were overriding `freeze-to-bytes` or `thaw-from-bytes` defaults to disable compression. Changed to map-based config to allow extra control (like :read-eval? as suggested by Luc Préfontaine). This change will also allow further additions in future without the need to break the API again.
This commit is contained in:
parent
1bcab7ede2
commit
8bec62767d
1 changed files with 18 additions and 17 deletions
|
|
@ -161,13 +161,13 @@
|
||||||
|
|
||||||
(defn freeze-to-bytes
|
(defn freeze-to-bytes
|
||||||
"Serializes x to a byte array and returns the array."
|
"Serializes x to a byte array and returns the array."
|
||||||
(^bytes [x] (freeze-to-bytes x true))
|
^bytes [x & {:keys [compress?]
|
||||||
(^bytes [x compress?]
|
:or {compress? true}}]
|
||||||
(let [ba (ByteArrayOutputStream.)
|
(let [ba (ByteArrayOutputStream.)
|
||||||
stream (DataOutputStream. ba)]
|
stream (DataOutputStream. ba)]
|
||||||
(freeze-to-stream! stream x)
|
(freeze-to-stream! stream x)
|
||||||
(let [ba (.toByteArray ba)]
|
(let [ba (.toByteArray ba)]
|
||||||
(if compress? (Snappy/compress ba) ba)))))
|
(if compress? (Snappy/compress ba) ba))))
|
||||||
|
|
||||||
;;;; Thawing
|
;;;; Thawing
|
||||||
|
|
||||||
|
|
@ -226,8 +226,8 @@
|
||||||
;; TODO Scheduled for Carmine version 1.0.0
|
;; TODO Scheduled for Carmine version 1.0.0
|
||||||
;; (defn thaw-from-stream!
|
;; (defn thaw-from-stream!
|
||||||
;; "Deserializes an object from given input stream."
|
;; "Deserializes an object from given input stream."
|
||||||
;; [data-input-stream]
|
;; [data-input-stream read-eval?]
|
||||||
;; (binding [*read-eval* false] ; For `read-string` injection safety - NB!!!
|
;; (binding [*read-eval* read-eval?]
|
||||||
;; (let [schema-header (thaw-from-stream!* data-input-stream)]
|
;; (let [schema-header (thaw-from-stream!* data-input-stream)]
|
||||||
;; (thaw-from-stream!* data-input-stream))))
|
;; (thaw-from-stream!* data-input-stream))))
|
||||||
|
|
||||||
|
|
@ -236,8 +236,8 @@
|
||||||
;; Carmine < 0.8.3 and haven't yet migrated their databases.
|
;; Carmine < 0.8.3 and haven't yet migrated their databases.
|
||||||
(defn thaw-from-stream!
|
(defn thaw-from-stream!
|
||||||
"Deserializes an object from given input stream."
|
"Deserializes an object from given input stream."
|
||||||
[data-input-stream]
|
[data-input-stream read-eval?]
|
||||||
(binding [*read-eval* false] ; For `read-string` injection safety - NB!!!
|
(binding [*read-eval* read-eval?]
|
||||||
(let [maybe-schema-header (thaw-from-stream!* data-input-stream)]
|
(let [maybe-schema-header (thaw-from-stream!* data-input-stream)]
|
||||||
(if (and (string? maybe-schema-header)
|
(if (and (string? maybe-schema-header)
|
||||||
(.startsWith ^String maybe-schema-header "\u0000~"))
|
(.startsWith ^String maybe-schema-header "\u0000~"))
|
||||||
|
|
@ -246,12 +246,13 @@
|
||||||
|
|
||||||
(defn thaw-from-bytes
|
(defn thaw-from-bytes
|
||||||
"Deserializes an object from given byte array."
|
"Deserializes an object from given byte array."
|
||||||
([ba] (thaw-from-bytes ba true))
|
[ba & {:keys [read-eval? compressed?]
|
||||||
([ba compressed?]
|
:or {read-eval? false ; For `read-string` injection safety - NB!!!
|
||||||
(->> (if compressed? (Snappy/uncompress ba) ba)
|
compressed? true}}]
|
||||||
|
(-> (if compressed? (Snappy/uncompress ba) ba)
|
||||||
(ByteArrayInputStream.)
|
(ByteArrayInputStream.)
|
||||||
(DataInputStream.)
|
(DataInputStream.)
|
||||||
(thaw-from-stream!))))
|
(thaw-from-stream! read-eval?)))
|
||||||
|
|
||||||
(def stress-data
|
(def stress-data
|
||||||
"Reference data used for tests & benchmarks."
|
"Reference data used for tests & benchmarks."
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue