Skip to content

Instantly share code, notes, and snippets.

@nbqx
Created February 28, 2014 09:10
Show Gist options
  • Save nbqx/9267817 to your computer and use it in GitHub Desktop.
Save nbqx/9267817 to your computer and use it in GitHub Desktop.
;; db => https://github.com/Factual/clj-leveldb
;; json => https://github.com/clojure/data.json
(def json-db (atom nil))
(defn val-encoder [m]
(let [s (json/write-str m)]
(byte-array (map byte s))))
(defn val-decoder [ba]
(let [s (byte-streams/to-string ba)
m (json/read-str s)
unzip (juxt (partial map (comp keyword first))
(partial map second))]
(apply zipmap (unzip m))))
(reset! json-db (db/create-db "json_db" {:key-decoder byte-streams/to-string
:val-encoder val-encoder
:val-decoder val-decoder}))
(db/put @json-db "a" {:name "unko" :data [1 2 3]})
(db/get @json-db "a") ;; => {:name "unko", :data [1 2 3]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment