[#122] Option to disable freezing and/or thawing of metadata

This commit is contained in:
Peter Taoussanis 2020-07-25 10:15:27 +02:00
parent 8f71638a19
commit db71943a5b
2 changed files with 45 additions and 6 deletions

View file

@ -35,7 +35,6 @@
;;;; TODO ;;;; TODO
;; - Performance would benefit from ^:static support / direct linking / etc. ;; - Performance would benefit from ^:static support / direct linking / etc.
;; - Ability to compile out metadata support?
;; - Auto cache keywords? When map keys? Configurable? Per-map ;; - Auto cache keywords? When map keys? Configurable? Per-map
;; (`cache-keys`)? Just rely on compression? ;; (`cache-keys`)? Just rely on compression?
@ -281,6 +280,8 @@
nil => default" nil => default"
nil) nil)
(enc/defonce ^:dynamic *incl-metadata?* "Include metadata when freezing/thawing?" true)
(def default-serializable-whitelist (def default-serializable-whitelist
"PRs welcome to add additional known-safe classes to default." "PRs welcome to add additional known-safe classes to default."
#{"[I" "[F" "[Z" "[B" "[C" "[D" "[S" "[J" #{"[I" "[F" "[Z" "[B" "[C" "[D" "[S" "[J"
@ -475,7 +476,7 @@
(extend-protocol IFreezable2 ; Must be a separate protocol (extend-protocol IFreezable2 ; Must be a separate protocol
clojure.lang.IMeta clojure.lang.IMeta
(-freeze-with-meta! [x ^DataOutput data-output] (-freeze-with-meta! [x ^DataOutput data-output]
(let [m (.meta x)] (let [m (when *incl-metadata?* (.meta x))]
(when m (when m
(write-id data-output id-meta) (write-id data-output id-meta)
(-freeze-without-meta! m data-output))) (-freeze-without-meta! m data-output)))
@ -1168,7 +1169,8 @@
(opt->bindings :freeze-fallback #'*freeze-fallback*) (opt->bindings :freeze-fallback #'*freeze-fallback*)
(opt->bindings :auto-freeze-compressor #'*auto-freeze-compressor*) (opt->bindings :auto-freeze-compressor #'*auto-freeze-compressor*)
(opt->bindings :serializable-whitelist #'*serializable-whitelist*) (opt->bindings :serializable-whitelist #'*serializable-whitelist*)
(opt->bindings :custom-readers #'*custom-readers*))] (opt->bindings :custom-readers #'*custom-readers*)
(opt->bindings :incl-metadata? #'*incl-metadata?*))]
(if-not bindings (if-not bindings
(f) ; Common case (f) ; Common case
@ -1204,7 +1206,7 @@
types, extend the Clojure reader or see `extend-freeze`." types, extend the Clojure reader or see `extend-freeze`."
([x] (freeze x nil)) ([x] (freeze x nil))
([x {:as opts ([x {:as opts
:keys [compressor encryptor password serializable-whitelist] :keys [compressor encryptor password serializable-whitelist incl-metadata?]
:or {compressor :auto :or {compressor :auto
encryptor aes128-gcm-encryptor}}] encryptor aes128-gcm-encryptor}}]
@ -1472,7 +1474,9 @@
id-false false id-false false
id-char (.readChar in) id-char (.readChar in)
id-meta (let [m (thaw-from-in! in)] id-meta (let [m (thaw-from-in! in)]
(with-meta (thaw-from-in! in) m)) (if *incl-metadata?*
(with-meta (thaw-from-in! in) m)
(do (thaw-from-in! in))))
id-cached-0 (thaw-cached 0 in) id-cached-0 (thaw-cached 0 in)
id-cached-1 (thaw-cached 1 in) id-cached-1 (thaw-cached 1 in)
@ -1656,7 +1660,7 @@
([^bytes ba ([^bytes ba
{:as opts {:as opts
:keys [v1-compatibility? compressor encryptor password :keys [v1-compatibility? compressor encryptor password
serializable-whitelist] serializable-whitelist incl-metadata?]
:or {compressor :auto :or {compressor :auto
encryptor :auto}}] encryptor :auto}}]

View file

@ -296,6 +296,41 @@
(is (= nippy/*serializable-whitelist* #{"base.1" "base.2" "add.1" "add.2"}) (is (= nippy/*serializable-whitelist* #{"base.1" "base.2" "add.1" "add.2"})
"JVM properties override initial serializable-whitelist value")) "JVM properties override initial serializable-whitelist value"))
;;;; Metadata
(deftest _metadata
(is
(:has-meta?
(meta
(nippy/thaw
(nippy/freeze (with-meta [] {:has-meta? true}) {:incl-metadata? true})
{:incl-metadata? true}
)))
"Metadata successfully included")
(is
(nil?
(meta
(nippy/thaw
(nippy/freeze (with-meta [] {:has-meta? true}) {:incl-metadata? true})
{:incl-metadata? false}
)))
"Metadata successfully excluded by thaw")
(is
(nil?
(meta
(nippy/thaw
(nippy/freeze (with-meta [] {:has-meta? true}) {:incl-metadata? false})
{:incl-metadata? true}
)))
"Metadata successfully excluded by freeze"))
;;;; Benchmarks ;;;; Benchmarks
(deftest _benchmarks (deftest _benchmarks