Malcolm Sparks
2017-01-20 13:04:29 UTC
Hi,
I've got to lazily stream a large amount of JSON using Cheshire's
generate-stream fn.
The closest I've managed to get is this:
(fn [ctx]
(let [pipe (java.nio.channels.Pipe/open)
writer (java.nio.channels.Channels/newWriter (.sink pipe) "utf-8")]
(doto (Thread.
(fn []
(cheshire.core/generate-stream some-obj writer)
(.close out)))
(.start))
(.source pipe)))
I've tried hard to use byte-streams/manifold to do this directly, but
couldn't figure it out, hence this posting.
The closest I've found is this code in byte-streams.clj :
(def-conversion ^{:cost 1.5} [(seq-of ByteBuffer) ReadableByteChannel]
[bufs]
(let [pipe (Pipe/open)
^WritableByteChannel sink (.sink pipe)
source (doto ^AbstractSelectableChannel (.source pipe)
(.configureBlocking true))]
(future
(try
(loop [s bufs]
(when (and (not (empty? s)) (.isOpen sink))
(let [buf (.duplicate ^ByteBuffer (first s))]
(.write sink buf)
(recur (rest s)))))
(finally
(.close sink))))
source))
But it seems I want the 'write' counterpart which doesn't exist. I don't
feel confident enough with the details to offer a PR.
Is there a better way of doing this in byte-streams/manifold as it is today?
Many thanks,
Malcolm
I've got to lazily stream a large amount of JSON using Cheshire's
generate-stream fn.
The closest I've managed to get is this:
(fn [ctx]
(let [pipe (java.nio.channels.Pipe/open)
writer (java.nio.channels.Channels/newWriter (.sink pipe) "utf-8")]
(doto (Thread.
(fn []
(cheshire.core/generate-stream some-obj writer)
(.close out)))
(.start))
(.source pipe)))
I've tried hard to use byte-streams/manifold to do this directly, but
couldn't figure it out, hence this posting.
The closest I've found is this code in byte-streams.clj :
(def-conversion ^{:cost 1.5} [(seq-of ByteBuffer) ReadableByteChannel]
[bufs]
(let [pipe (Pipe/open)
^WritableByteChannel sink (.sink pipe)
source (doto ^AbstractSelectableChannel (.source pipe)
(.configureBlocking true))]
(future
(try
(loop [s bufs]
(when (and (not (empty? s)) (.isOpen sink))
(let [buf (.duplicate ^ByteBuffer (first s))]
(.write sink buf)
(recur (rest s)))))
(finally
(.close sink))))
source))
But it seems I want the 'write' counterpart which doesn't exist. I don't
feel confident enough with the details to offer a PR.
Is there a better way of doing this in byte-streams/manifold as it is today?
Many thanks,
Malcolm
--
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Aleph" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aleph-lib+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.