Experimental: add inspect-ba util for inspecting possibly-frozen data

This commit is contained in:
Peter Taoussanis 2013-12-03 14:36:33 +07:00
parent c130e41d58
commit 1a7812522c
3 changed files with 46 additions and 0 deletions

View file

@ -1,3 +1,17 @@
## WIP / 2013-12-03
WIP
### Features
* Added experimental `inspect-ba` fn for examining data possibly frozen by Nippy.
### Changes
*
### Fixes
*
## v2.4.1 → v2.5.0
* Refactored standard Freezable protocol implementations to de-emphasise interfaces as a matter of hygiene, Ref. http://goo.gl/IFXzvh.
* BETA STATUS: Added an additional (pre-Reader) Serializable fallback. This should greatly extend the number of out-the-box-serializable types.

View file

@ -266,6 +266,8 @@
(defn- thaw-from-stream
[^DataInputStream s]
(let [type-id (.readByte s)]
#_(when debug-mode?
(println (format "DEBUG - thawing type-id: %s" type-id)))
(utils/case-eval type-id
id-reader (edn/read-string {:readers *data-readers*} (read-utf8 s))
@ -531,6 +533,34 @@
:exception (try (/ 1 0) (catch Exception e e))
:ex-info (ex-info "ExInfo" {:data "data"})}))
;;;; Data recovery/analysis
(defn inspect-ba "Alpha - subject to change."
[ba & [thaw-opts]]
(if-not (utils/bytes? ba) :not-ba
(let [[first2bytes nextbytes] (utils/ba-split ba 2)
known-wrapper
(cond
(utils/ba= first2bytes (.getBytes "\u0000<" "UTF8")) :carmine/bin
(utils/ba= first2bytes (.getBytes "\u0000>" "UTF8")) :carmine/clj)
unwrapped-ba (if known-wrapper nextbytes ba)
[data-ba nippy-header] (or (try-parse-header unwrapped-ba)
[unwrapped-ba :no-header])]
{:known-wrapper known-wrapper
:nippy2-header nippy-header ; Nippy v1.x didn't have a header
:thawable? (try (thaw unwrapped-ba thaw-opts) true
(catch Exception _ false))
:unwrapped-ba unwrapped-ba
:data-ba data-ba
:unwrapped-size (alength ^bytes unwrapped-ba)
:ba-size (alength ^bytes ba)
:data-size (alength ^bytes data-ba)})))
(comment (inspect-ba (freeze "hello"))
(seq (:data-ba (inspect-ba (freeze "hello")))))
;;;; Deprecated API
(defn- assert-legacy-args [compressor password]

View file

@ -74,6 +74,8 @@
(comment (memoized nil +)
(memoized nil + 5 12))
(def ^:const bytes-class (Class/forName "[B"))
(defn bytes? [x] (instance? bytes-class x))
(defn ba= [^bytes x ^bytes y] (java.util.Arrays/equals x y))
(defn ba-concat ^bytes [^bytes ba1 ^bytes ba2]