Hey Folks,
I am writing a thin Clojure wrapper around
leveldbjni https://github.com/fusesource/leveldbjni
Leveldbjni provides an iterator to walk over all key, value pairs in the
database. I am trying to translate this iterator functionality in to a
Clojure lazy sequence. I've built my function, but I hit a couple of issues
in the process that I would like to float by the wise Clojure sages on this
list...
1. I seemed unable to use iterator-seq on the iterator. I assumed this was
because leveldbjni is "faking" an iterator interface due to cpp/jni
interop. Can anyone confirm this?
2. I need to close the iterator to free the resource. This seems to make it
difficult to build an efficient lazy sequence? One option is to "open,
seek, close" to generate the next pair in the sequence but I'm pretty sure
this would turn an O(N) iteration in to an O(N^2) iteration. Also, the DB
will get large so I'd rather not slurp everything in to memory. Maybe I
could slurp in chunks? Is anyone able to add/improve/comment on this?
At risk of diverting the focus of this post to my mediocre Clojure code,
here's my function to date for completeness sake. Please note that I am a
Clojure noob :)
(defn pairs
"Lazy sequence of key, value pairs in the database"
([db]
(let [iter (.iterator db)]
(.seekToFirst iter)
(pairs db iter)))
([db iter]
(lazy-seq
(if (.hasNext iter)
(let [pair (.peekNext iter)
pair-key (JniDBFactory/asString (.getKey pair))
pair-value (JniDBFactory/asString (.getValue pair))
_ (.next iter)]
(cons [pair-key pair-value] (pairs db iter)))
nil))))
All help greatly appreciated.
Many thanks,
Alex.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en