Skip to content

Commit 5f66e2a

Browse files
committed
Add Java serialization utility and dval helpers.
1 parent 24ab49a commit 5f66e2a

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/clojure/parkour/io/dval.clj

+5-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"Generate distinct distcache-entry name for `source`."
7878
[source] (str (gensym "dval-") "-" (-> source fs/path .getName)))
7979

80-
(defn dval
80+
(defn ^:private dval
8181
"Return a dval which locally holds `value` and remotely will deserialize by
8282
applying var `readv` to the concatenation of `params` and distributed copies of
8383
`sources`."
@@ -119,3 +119,7 @@ serialization path."
119119
(defn edn-dval
120120
"EDN-serialize `value` to a transient location and yield a wrapping dval."
121121
[value] (transient-dval util/edn-spit #'util/edn-slurp value))
122+
123+
(defn jser-dval
124+
"Java-serialize `value` to a transient location and yield a wrapping dval."
125+
[value] (transient-dval util/jser-spit #'util/jser-slurp value))

src/clojure/parkour/util.clj

+22-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(ns parkour.util
22
(:require [clojure.edn :as edn]
33
[clojure.java.io :as io])
4-
(:import [java.io PushbackReader Writer]
4+
(:import [java.io PushbackReader Writer ObjectInputStream ObjectOutputStream]
55
[clojure.lang IPending]))
66

77
(defmacro ignore-errors
@@ -35,6 +35,16 @@ already an instance of `c`."
3535
"Return a new map made by mapping `f` over the values of `m`."
3636
[f m] (into {} (map (fn [[k v]] [k (f v)]) m)))
3737

38+
(defn realized-seq
39+
"Seq of only the already-realized portion of `coll`."
40+
[coll]
41+
(if-not (instance? IPending coll)
42+
(seq coll)
43+
(lazy-seq
44+
(if (realized? coll)
45+
(cons (first coll)
46+
(realized-seq (rest coll)))))))
47+
3848
(defn var-str
3949
"String fully-qualified name of a var."
4050
[v] (subs (pr-str v) 2))
@@ -104,12 +114,14 @@ already an instance of `c`."
104114
(with-open [f (PushbackReader. (apply io/reader f opts))]
105115
(edn/read {:readers *data-readers*} f)))
106116

107-
(defn realized-seq
108-
"Seq of only the already-realized portion of `coll`."
109-
[coll]
110-
(if-not (instance? IPending coll)
111-
(seq coll)
112-
(lazy-seq
113-
(if (realized? coll)
114-
(cons (first coll)
115-
(realized-seq (rest coll)))))))
117+
(defn jser-spit
118+
"Like `split`, but emits `content` to `f` via Java serialization."
119+
[f content & opts]
120+
(with-open [oos (ObjectOutputStream. (apply io/output-stream f opts))]
121+
(.writeObject oos content)))
122+
123+
(defn jser-slurp
124+
"Like `slurp`, but reads content from `f` via Java serialization."
125+
[f & opts]
126+
(with-open [ois (ObjectInputStream. (apply io/input-stream f opts))]
127+
(.readObject ois)))

0 commit comments

Comments
 (0)