jankotek / JDBM3

Embedded Key Value Java Database
367 stars 94 forks source link

How to open a map if it exists, create it if not? #25

Closed KenMcDonald closed 12 years ago

KenMcDonald commented 12 years ago

Sorry for posting this as an issue, but I haven't found a forum for jdbm 3...

I'd like to open a map as soon as my program starts, by calling a single method that will open that map if it exists already in the given db file, or will create the map if it does not exist. However, if I try to do this using getTreeMap (for example), the initial call fails since the map doesn't exist; and if I try to do it using createTreeMap, calls after the first fail, as the map already exists.

So far, the only way I can see to do this is to use a try statement, which seems extremely ugly to me.

Am I missing something obvious in the API?

Thanks, Ken (email: ykkenmcd at gmail.com)

jankotek commented 12 years ago

There is method DB.getCollections() (or similar name) which returns map of all existing collection. So you should use something like

Map m = db.getCollections().get("map")
if(m == null)
      m = db.createTreeMap("map")