Skip to content

Commit

Permalink
Experimental: add inspect-ba util for inspecting possibly-frozen data
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaoussanis committed Dec 3, 2013
1 parent c130e41 commit 1a78125
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
30 changes: 30 additions & 0 deletions src/taoensso/nippy.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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]
Expand Down
2 changes: 2 additions & 0 deletions src/taoensso/nippy/utils.clj
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 1a78125

Please sign in to comment.