Search code examples
javaclojurelmdb

Using lmdbjava in Clojure


I'm trying to use lmdbjava in Clojure, but I'm struggling.

(import '[org.lmdbjava Env])
(def path (clojure.java.io/file "/tmp"))
(.open (.setMaxDbs (.setMapSize (Env/create) 10485760) 1) path)

(p.s. I realise there are cleaner ways. This is just for testing purposes.)

This is the error:

IllegalArgumentException No matching method found: open for class java.lang.Class  clojure.lang.Reflector.invokeMatchingMethod (Reflector.java:53)

I've also tried this:

(.open (.setMaxDbs (.setMapSize (Env/create) 10485760) 1) path org.lmdbjava.EnvFlags/MDB_NOLOCK)

and this:

(.. (Env/create) (setMapSize 10485760) (setMaxDbs 1) (open path org.lmdbjava.EnvFlags/MDB_NOLOCK))

And I get this error:

ClassCastException org.lmdbjava.EnvFlags (in module: Unnamed Module) cannot be cast to [Lorg.lmdbjava.EnvFlags; (in module: Unnamed Module)  user/eval1339 (form-init2868059116743223586.clj:1)

I realise I'm probably doing something daft, because I'm new to both Java and Clojure. Any help would be greatly appreciated!

By the way, this is the tutorial I'm following:

https://github.com/lmdbjava/lmdbjava/blob/master/src/test/java/org/lmdbjava/TutorialTest.java

Thanks!


Solution

  • From the signature of the Env.Builder class:

    Env<T>  open(File path, int mode, EnvFlags... flags)
    

    you also have to supply a EnvFlags varargs parameter. This is how you do it in Clojure:

    (.open (.setMaxDbs (.setMapSize (Env/create) 10485760) 1) path (into-array org.lmdbjava.EnvFlags []))
    

    Also see How to handle java variable length arguments in clojure?