Skip to content

Commit 2a4347c

Browse files
committed
[new] [#178] Add native support for more arrays: strings, longs, ints
1 parent 147dcba commit 2a4347c

File tree

2 files changed

+68
-38
lines changed

2 files changed

+68
-38
lines changed

src/taoensso/nippy.clj

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,6 @@
145145
51 [:reader-md [[:bytes {:read 2}]]]
146146
52 [:reader-lg [[:bytes {:read 4}]]]
147147

148-
53 [:bytes-0 []]
149-
7 [:bytes-sm [[:bytes {:read 1}]]]
150-
15 [:bytes-md [[:bytes {:read 2}]]]
151-
2 [:bytes-lg [[:bytes {:read 4}]]]
152-
153148
17 [:vec-0 []]
154149
113 [:vec-2 [[:elements 2]]]
155150
114 [:vec-3 [[:elements 3]]]
@@ -182,7 +177,6 @@
182177
28 [:sorted-set-lg [[:elements {:read 4}]]]
183178
31 [:sorted-map-lg [[:elements {:read 4 :multiplier 2}]]]
184179
26 [:queue-lg [[:elements {:read 4}]]]
185-
115 [:objects-lg [[:elements {:read 4}]]]
186180

187181
25 [:meta [[:elements 1]]]
188182
58 [:regex [[:elements 1]]]
@@ -196,6 +190,16 @@
196190
70 [:ratio [[:bytes {:read 4}]
197191
[:bytes {:read 4}]]]
198192

193+
;; Arrays
194+
53 [:byte-array-0 []]
195+
7 [:byte-array-sm [[:elements {:read 1}]]]
196+
15 [:byte-array-md [[:elements {:read 2}]]]
197+
2 [:byte-array-lg [[:elements {:read 4}]]]
198+
109 [:int-array-lg [[:elements {:read 4}]]] ; Added vX.Y.Z (YYYY-MM-DD)
199+
108 [:long-array-lg [[:elements {:read 4}]]] ; Added vX.Y.Z (YYYY-MM-DD)
200+
107 [:string-array-lg [[:elements {:read 4}]]] ; Added vX.Y.Z (YYYY-MM-DD)
201+
115 [:object-array-lg [[:elements {:read 4}]]]
202+
199203
;; Serializable
200204
75 [:sz-quarantined-sm [[:bytes {:read 1}] [:elements 1]]]
201205
76 [:sz-quarantined-md [[:bytes {:read 2}] [:elements 1]]]
@@ -641,15 +645,23 @@
641645
(defn- write-bytes [^DataOutput out ^bytes ba]
642646
(let [len (alength ba)]
643647
(if (zero? len)
644-
(write-id out id-bytes-0)
648+
(write-id out id-byte-array-0)
645649
(do
646650
(enc/cond
647-
(sm-count? len) (do (write-id out id-bytes-sm) (write-sm-count out len))
648-
(md-count? len) (do (write-id out id-bytes-md) (write-md-count out len))
649-
:else (do (write-id out id-bytes-lg) (write-lg-count out len)))
651+
(sm-count? len) (do (write-id out id-byte-array-sm) (write-sm-count out len))
652+
(md-count? len) (do (write-id out id-byte-array-md) (write-md-count out len))
653+
:else (do (write-id out id-byte-array-lg) (write-lg-count out len)))
650654

651655
(.write out ba 0 len)))))
652656

657+
(defmacro ^:private -run! [proc coll] `(do (reduce #(~proc %2) nil ~coll) nil))
658+
(defmacro ^:private -run-kv! [proc m] `(do (reduce-kv #(~proc %2 %3) nil ~m) nil))
659+
660+
(defn- write-array-lg [^DataOutput out array array-len id]
661+
(write-id out id)
662+
(write-lg-count out array-len)
663+
(-run! (fn [in] (-freeze-with-meta! in out)) array))
664+
653665
(defn- write-biginteger [out ^BigInteger n] (write-bytes-lg out (.toByteArray n)))
654666

655667
(defn- write-str-sm* [^DataOutput out ^String s] (write-bytes-sm* out (.getBytes s StandardCharsets/UTF_8)))
@@ -729,9 +741,6 @@
729741
(<= y range-uint) (do (write-id out id-long-neg-lg) (.writeInt out (+ y Integer/MIN_VALUE)))
730742
:else (do (write-id out id-long-xl) (.writeLong out n))))))
731743

732-
(defmacro ^:private -run! [proc coll] `(do (reduce #(~proc %2) nil ~coll) nil))
733-
(defmacro ^:private -run-kv! [proc m] `(do (reduce-kv #(~proc %2 %3) nil ~m) nil))
734-
735744
(defn- write-vec [^DataOutput out v]
736745
(let [cnt (count v)]
737746
(if (zero? cnt)
@@ -879,12 +888,6 @@
879888

880889
(-run! (fn [in] (-freeze-with-meta! in out)) s)))))
881890

882-
(defn- write-objects [^DataOutput out ^objects ary]
883-
(let [len (alength ary)]
884-
(write-id out id-objects-lg)
885-
(write-lg-count out len)
886-
(-run! (fn [in] (-freeze-with-meta! in out)) ary)))
887-
888891
(defn- write-serializable [^DataOutput out x ^String class-name]
889892
(when-debug (println (str "write-serializable: " (type x))))
890893
(let [class-name-ba (.getBytes class-name StandardCharsets/UTF_8)
@@ -1096,8 +1099,6 @@
10961099
(.writeLong out (.getLeastSignificantBits x))))
10971100

10981101
(freezer Boolean nil true (if (boolean x) (write-id out id-true) (write-id out id-false)))
1099-
(freezer (Class/forName "[B") nil true (write-bytes out x))
1100-
(freezer (Class/forName "[Ljava.lang.Object;") nil true (write-objects out x))
11011102
(freezer String nil true (write-str out x))
11021103
(freezer Keyword nil true (write-kw out x))
11031104
(freezer Symbol nil true (write-sym out x))
@@ -1107,6 +1108,15 @@
11071108
(do (write-id out id-double-0))
11081109
(do (write-id out id-double) (.writeDouble out x))))
11091110

1111+
;; Arrays
1112+
(freezer (Class/forName "[B") nil true (write-bytes out x))
1113+
(freezer (Class/forName "[Ljava.lang.Object;") nil true (write-array-lg out x (alength ^"[Ljava.lang.Object;" x) id-object-array-lg))
1114+
1115+
(when (impl/target-release>= 350)
1116+
(freezer (Class/forName "[I") nil true (write-array-lg out x (alength ^"[I" x) id-int-array-lg))
1117+
(freezer (Class/forName "[J") nil true (write-array-lg out x (alength ^"[J" x) id-long-array-lg))
1118+
(freezer (Class/forName "[Ljava.lang.String;") nil true (write-array-lg out x (alength ^"[Ljava.lang.String;" x) id-string-array-lg)))
1119+
11101120
(freezer PersistentQueue nil true (write-counted-coll out id-queue-lg x))
11111121
(freezer PersistentTreeSet nil true (write-counted-coll out id-sorted-set-lg x))
11121122
(freezer PersistentTreeMap nil true (write-kvs out id-sorted-map-lg x))
@@ -1330,10 +1340,21 @@
13301340
([^DataInput in len] (let [ba (byte-array len)] (.readFully in ba 0 len) ba))
13311341
([^DataInput in ]
13321342
(enc/case-eval (.readByte in)
1333-
id-bytes-0 (byte-array 0)
1334-
id-bytes-sm (read-bytes in (read-sm-count in))
1335-
id-bytes-md (read-bytes in (read-md-count in))
1336-
id-bytes-lg (read-bytes in (read-lg-count in)))))
1343+
id-byte-array-0 (byte-array 0)
1344+
id-byte-array-sm (read-bytes in (read-sm-count in))
1345+
id-byte-array-md (read-bytes in (read-md-count in))
1346+
id-byte-array-lg (read-bytes in (read-lg-count in)))))
1347+
1348+
(defmacro ^:private read-array [in thaw-type array array-type]
1349+
(let [thawed-sym (with-meta 'thawed-sym {:tag thaw-type})
1350+
array-sym (with-meta 'array-sym {:tag array-type})]
1351+
`(let [~array-sym ~array]
1352+
(enc/reduce-n
1353+
(fn [_# idx#]
1354+
(let [~thawed-sym (thaw-from-in! ~in)]
1355+
(aset ~'array-sym idx# ~'thawed-sym)))
1356+
nil (alength ~'array-sym))
1357+
~'array-sym)))
13371358

13381359
(defn- read-str-sm* [^DataInput in] (String. ^bytes (read-bytes in (read-sm-count* in)) StandardCharsets/UTF_8))
13391360
(defn- read-str-sm [^DataInput in] (String. ^bytes (read-bytes in (read-sm-count in)) StandardCharsets/UTF_8))
@@ -1382,12 +1403,6 @@
13821403
(let [rf rf2 ] (rf (enc/reduce-n (fn [acc _] (rf acc (thaw-from-in! in) (thaw-from-in! in))) init n)))))))
13831404

13841405
(defn- read-kvs-depr [to ^DataInput in] (read-kvs-into to in (quot (.readInt in) 2)))
1385-
(defn- read-objects [^objects ary ^DataInput in]
1386-
(enc/reduce-n
1387-
(fn [^objects ary i]
1388-
(aset ary i (thaw-from-in! in))
1389-
ary)
1390-
ary (alength ary)))
13911406

13921407
(def ^:private class-method-sig (into-array Class [IPersistentMap]))
13931408

@@ -1580,12 +1595,15 @@
15801595
id-cached-sm (thaw-cached (read-sm-count in) in)
15811596
id-cached-md (thaw-cached (read-md-count in) in)
15821597

1583-
id-bytes-0 (byte-array 0)
1584-
id-bytes-sm (read-bytes in (read-sm-count in))
1585-
id-bytes-md (read-bytes in (read-md-count in))
1586-
id-bytes-lg (read-bytes in (read-lg-count in))
1598+
id-byte-array-0 (byte-array 0)
1599+
id-byte-array-sm (read-bytes in (read-sm-count in))
1600+
id-byte-array-md (read-bytes in (read-md-count in))
1601+
id-byte-array-lg (read-bytes in (read-lg-count in))
15871602

1588-
id-objects-lg (read-objects (object-array (read-lg-count in)) in)
1603+
id-int-array-lg (read-array in int (int-array (read-lg-count in)) "[I")
1604+
id-long-array-lg (read-array in long (long-array (read-lg-count in)) "[J")
1605+
id-string-array-lg (read-array in String (make-array String (read-lg-count in)) "[Ljava.lang.String;")
1606+
id-object-array-lg (read-array in Object (object-array (read-lg-count in)) "[Ljava.lang.Object;")
15891607

15901608
id-str-0 ""
15911609
id-str-sm* (read-str in (read-sm-count* in))
@@ -2039,9 +2057,17 @@
20392057
:uri (java.net.URI. "https://clojure.org")
20402058
:defrecord (StressRecord. "data")
20412059
:deftype (StressType. "data")
2060+
20422061
:bytes (byte-array [(byte 1) (byte 2) (byte 3)])
20432062
:objects (object-array [1 "two" {:data "data"}])
20442063

2064+
;; TODO (target-release>= 350)
2065+
;; :byte-array (byte-array [(byte 1) (byte 2) (byte 3) (byte 4)])
2066+
;; :int-array (int-array [1 2 3 4])
2067+
;; :long-array (long-array [1 2 3 4])
2068+
;; :object-array (object-array [1 "two" {:data "data"}])
2069+
;; :string-array (into-array String ["a" "b" "c"])
2070+
20452071
:util-date (java.util.Date. 1577884455500)
20462072
:sql-date (java.sql.Date. 1577884455500)
20472073
:instant (enc/compile-if java.time.Instant (java.time.Instant/parse "2020-01-01T13:14:15.50Z") ::skip)

wiki/1 Getting-started.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ As an example of what it can do, let's take a look at Nippy's own reference [str
6666
:uri (java.net.URI. "https://clojure.org")
6767
:defrecord (nippy/StressRecord. "data")
6868
:deftype (nippy/StressType. "data")
69-
:bytes (byte-array [(byte 1) (byte 2) (byte 3)])
70-
:objects (object-array [1 "two" {:data "data"}])
69+
70+
:byte-array (byte-array [(byte 1) (byte 2) (byte 3) (byte 4)])
71+
:int-array (int-array [1 2 3 4])
72+
:long-array (long-array [1 2 3 4])
73+
:object-array (object-array [1 "two" {:data "data"}])
74+
:string-array (into-array String ["a" "b" "c"])
7175

7276
:util-date (java.util.Date. 1577884455500)
7377
:sql-date (java.sql.Date. 1577884455500)

0 commit comments

Comments
 (0)