Writing large map into a file

clojure

#1

I’m trying to write large map into a file, and for some reason it doesn’t save the whole thing. It writes a map that ends with ellipsis like that {:key-n val-n, ...}. I tried switching from spit to doing this:

(with-open [w (clojure.java.io/writer filename)]
      (binding [*out* w] (pr my-large-map)))

Yet that didn’t help. What am I missing? I mean the map is not that big, it actually contains less than 200 elements, but the keys are very long strings. Could that be some kind repl/nrepl/CIDER limitation?


#2

It seems due to *print-length* limitation. I think CIDER makes it 100 by default.


#3

Clojure defaults to no limit. So this will work as normal outside of Cider.

I opened an issue recently on Cider: https://github.com/clojure-emacs/cider/issues/2507 for this.


#4

Out of curiosity: why not just use spit and pr-str?


#5

Seems it was too see if spit was the problem.


#6

Out of curiosity: why not just use spit and pr-str ?

pr-str will materialize a java.lang.String buffer containing the entire contents of the file eagerly. pr or prn however just streams writes to a writer, which lets you bypass the buffer and go “straight” to the disk. It’s just a memory/IO optimization that can be relevant for “lots” of data.


#7

Good point about eagerness vs. streaming.

Also I should have properly read the first post, the OP explicitly mentioned they switched from spit:man_facepalming:


#8
(binding [*print-length* nil]
  ;; do all your spits here...
  )