I came across MapDB recently and I have to share my find with the world. I like to consider my self as well informed about frameworks, tools and everything related with Java, but this little gem just slipped under my radar.

So what is MapDB? MapDB provides Java Maps, Sets, Lists, Queues and other collections backed by off-heap or on-disk storage. It is a hybrid between java collection framework and embedded database engine. It is free and open-source under Apache license.

They provides good documentation and good examples about it. Have a look at their GitHub page. Interesting thing is that MapDB is written in Kotlin.

So let’s have a look a some code:

 
DB db = DBMaker.memoryDB().make();
ConcurrentMap map = db.hashMap("map").createOrOpen();
map.put("key", "value");

This code is of in memory MapDB so any data that is inside won’t survive shutdown. So you have a option to store the collections in a file, if you would like to save data between restarts. Code for that looks like so:

 
DB db = DBMaker.fileDB("db_file").make();
ConcurrentMap map = db.hashMap("map").createOrOpen();
map.put("key", "value");

So just replace DBMaker.memoryDB().make() into DBMaker.fileDB(“db_file”).make() where as as a parameter you provide the name of the file to which your collection will be stored.

Transactions are supported out of the box. Also MapDB has Pump for fast bulk import of collections which is much faster than to Map.put()

By default, MapDB uses generic serialization, which can serialize any data type but more faster and more memory efficient is to use specialized serializers. You can enable faster memory-mapped files on 64bit operating systems like so:

 
DB db = DBMaker
        .fileDB("file.db")
        .fileMmapEnable()
        .make();
ConcurrentMap<String,Long> map = db
        .hashMap("map", Serializer.STRING, Serializer.LONG)
        .createOrOpen();
map.put("something", 111L);

db.close();

Currently at the time of writing this post, MapDB is on version 3.0.3 and in their road map they plan to do some more great things with upcoming versions. If you are interested what the future brings to MapDB have a look at their blog post.

I plan to use MapDB for a future blog post, but more on that soon.