Housekeeping
This commit is contained in:
parent
cdf3ad736f
commit
26f32c7a95
1 changed files with 35 additions and 45 deletions
|
|
@ -65,6 +65,7 @@
|
||||||
(def ^:const id-ratio (int 70))
|
(def ^:const id-ratio (int 70))
|
||||||
|
|
||||||
(def ^:const id-record (int 80))
|
(def ^:const id-record (int 80))
|
||||||
|
;; (def ^:const id-type (int 81)) ; TODO
|
||||||
|
|
||||||
(def ^:const id-date (int 90))
|
(def ^:const id-date (int 90))
|
||||||
(def ^:const id-uuid (int 91))
|
(def ^:const id-uuid (int 91))
|
||||||
|
|
@ -101,38 +102,29 @@
|
||||||
[^DataOutputStream data-output-stream x & _]
|
[^DataOutputStream data-output-stream x & _]
|
||||||
(freeze-to-stream data-output-stream x))
|
(freeze-to-stream data-output-stream x))
|
||||||
|
|
||||||
(defmacro ^:private freezer
|
(defmacro ^:private freezer [type id & body]
|
||||||
"Helper to extend Freezable protocol."
|
|
||||||
[type id & body]
|
|
||||||
`(extend-type ~type
|
`(extend-type ~type
|
||||||
Freezable
|
Freezable
|
||||||
(~'freeze-to-stream* [~'x ~(with-meta 's {:tag 'DataOutputStream})]
|
(~'freeze-to-stream* [~'x ~(with-meta 's {:tag 'DataOutputStream})]
|
||||||
(write-id ~'s ~id)
|
(write-id ~'s ~id)
|
||||||
~@body)))
|
~@body)))
|
||||||
|
|
||||||
(defmacro ^:private coll-freezer
|
(defmacro ^:private freezer-coll [type id & body]
|
||||||
"Extends Freezable to simple collection types."
|
|
||||||
[type id & body]
|
|
||||||
`(freezer ~type ~id
|
`(freezer ~type ~id
|
||||||
(if (counted? ~'x)
|
(if (counted? ~'x)
|
||||||
(do
|
(do (.writeInt ~'s (count ~'x))
|
||||||
(.writeInt ~'s (count ~'x))
|
(doseq [i# ~'x] (freeze-to-stream ~'s i#)))
|
||||||
(doseq [i# ~'x] (freeze-to-stream ~'s i#)))
|
|
||||||
(let [bas# (ByteArrayOutputStream.)
|
(let [bas# (ByteArrayOutputStream.)
|
||||||
s# (DataOutputStream. bas#)
|
s# (DataOutputStream. bas#)
|
||||||
cnt# (reduce
|
cnt# (reduce (fn [cnt# i#]
|
||||||
(fn [cnt# i#]
|
(freeze-to-stream s# i#)
|
||||||
(freeze-to-stream! s# i#)
|
(unchecked-inc cnt#))
|
||||||
(unchecked-inc cnt#))
|
0 ~'x)
|
||||||
0
|
|
||||||
~'x)
|
|
||||||
ba# (.toByteArray bas#)]
|
ba# (.toByteArray bas#)]
|
||||||
(.writeInt ~'s cnt#)
|
(.writeInt ~'s cnt#)
|
||||||
(.write ~'s ba# 0 (alength ba#))))))
|
(.write ~'s ba# 0 (alength ba#))))))
|
||||||
|
|
||||||
(defmacro ^:private kv-freezer
|
(defmacro ^:private freezer-kvs [type id & body]
|
||||||
"Extends Freezable to key-value collection types."
|
|
||||||
[type id & body]
|
|
||||||
`(freezer ~type ~id
|
`(freezer ~type ~id
|
||||||
(.writeInt ~'s (* 2 (count ~'x)))
|
(.writeInt ~'s (* 2 (count ~'x)))
|
||||||
(doseq [kv# ~'x]
|
(doseq [kv# ~'x]
|
||||||
|
|
@ -153,15 +145,15 @@
|
||||||
(write-utf8 s (.getName (class x)))
|
(write-utf8 s (.getName (class x)))
|
||||||
(freeze-to-stream s (into {} x)))
|
(freeze-to-stream s (into {} x)))
|
||||||
|
|
||||||
(coll-freezer PersistentQueue id-queue)
|
(freezer-coll PersistentQueue id-queue)
|
||||||
(coll-freezer PersistentTreeSet id-sorted-set)
|
(freezer-coll PersistentTreeSet id-sorted-set)
|
||||||
(kv-freezer PersistentTreeMap id-sorted-map)
|
(freezer-kvs PersistentTreeMap id-sorted-map)
|
||||||
|
|
||||||
(coll-freezer IPersistentList id-list)
|
(freezer-coll IPersistentList id-list)
|
||||||
(coll-freezer IPersistentVector id-vector)
|
(freezer-coll IPersistentVector id-vector)
|
||||||
(coll-freezer IPersistentSet id-set)
|
(freezer-coll IPersistentSet id-set)
|
||||||
(kv-freezer APersistentMap id-map)
|
(freezer-kvs APersistentMap id-map)
|
||||||
(coll-freezer ISeq id-seq)
|
(freezer-coll ISeq id-seq)
|
||||||
|
|
||||||
(freezer Byte id-byte (.writeByte s x))
|
(freezer Byte id-byte (.writeByte s x))
|
||||||
(freezer Short id-short (.writeShort s x))
|
(freezer Short id-short (.writeShort s x))
|
||||||
|
|
@ -186,7 +178,9 @@
|
||||||
(.writeLong s (.getLeastSignificantBits x)))
|
(.writeLong s (.getLeastSignificantBits x)))
|
||||||
|
|
||||||
;; Use Clojure's own reader as final fallback
|
;; Use Clojure's own reader as final fallback
|
||||||
(freezer Object id-reader (write-bytes s (.getBytes (pr-str x) "UTF-8")))
|
(freezer Object id-reader
|
||||||
|
;; (println (format "DEBUG - Using reader for type: %s" (type x)))
|
||||||
|
(write-bytes s (.getBytes (pr-str x) "UTF-8")))
|
||||||
|
|
||||||
(def ^:private head-meta-id (reduce-kv #(assoc %1 %3 %2) {} head-meta))
|
(def ^:private head-meta-id (reduce-kv #(assoc %1 %3 %2) {} head-meta))
|
||||||
|
|
||||||
|
|
@ -232,16 +226,12 @@
|
||||||
(defmacro ^:private read-biginteger [s] `(BigInteger. (read-bytes ~s)))
|
(defmacro ^:private read-biginteger [s] `(BigInteger. (read-bytes ~s)))
|
||||||
(defmacro ^:private read-utf8 [s] `(String. (read-bytes ~s) "UTF-8"))
|
(defmacro ^:private read-utf8 [s] `(String. (read-bytes ~s) "UTF-8"))
|
||||||
|
|
||||||
(defmacro ^:private coll-thaw "Thaws simple collection types."
|
(defmacro ^:private read-coll [s coll]
|
||||||
[s coll]
|
`(let [s# ~s] (utils/repeatedly-into ~coll (.readInt s#) (thaw-from-stream s#))))
|
||||||
`(let [s# ~s]
|
|
||||||
(utils/repeatedly-into ~coll (.readInt s#) (thaw-from-stream s#))))
|
|
||||||
|
|
||||||
(defmacro ^:private coll-thaw-kvs "Thaws key-value collection types."
|
(defmacro ^:private read-kvs [s coll]
|
||||||
[s coll]
|
`(let [s# ~s] (utils/repeatedly-into ~coll (/ (.readInt s#) 2)
|
||||||
`(let [s# ~s]
|
[(thaw-from-stream s#) (thaw-from-stream s#)])))
|
||||||
(utils/repeatedly-into ~coll (/ (.readInt s#) 2)
|
|
||||||
[(thaw-from-stream s#) (thaw-from-stream s#)])))
|
|
||||||
|
|
||||||
(declare ^:private custom-readers)
|
(declare ^:private custom-readers)
|
||||||
|
|
||||||
|
|
@ -259,15 +249,15 @@
|
||||||
id-string (read-utf8 s)
|
id-string (read-utf8 s)
|
||||||
id-keyword (keyword (read-utf8 s))
|
id-keyword (keyword (read-utf8 s))
|
||||||
|
|
||||||
id-queue (coll-thaw s (PersistentQueue/EMPTY))
|
id-queue (read-coll s (PersistentQueue/EMPTY))
|
||||||
id-sorted-set (coll-thaw s (sorted-set))
|
id-sorted-set (read-coll s (sorted-set))
|
||||||
id-sorted-map (coll-thaw-kvs s (sorted-map))
|
id-sorted-map (read-kvs s (sorted-map))
|
||||||
|
|
||||||
id-list (into '() (rseq (coll-thaw s [])))
|
id-list (into '() (rseq (read-coll s [])))
|
||||||
id-vector (coll-thaw s [])
|
id-vector (read-coll s [])
|
||||||
id-set (coll-thaw s #{})
|
id-set (read-coll s #{})
|
||||||
id-map (coll-thaw-kvs s {})
|
id-map (read-kvs s {})
|
||||||
id-seq (coll-thaw s [])
|
id-seq (read-coll s [])
|
||||||
|
|
||||||
id-meta (let [m (thaw-from-stream s)] (with-meta (thaw-from-stream s) m))
|
id-meta (let [m (thaw-from-stream s)] (with-meta (thaw-from-stream s) m))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue