Quick Introduction

MapDB is flexible, with many configuration options. But in most cases, it is configured with just a few lines of code.

TODO more resources: cheat sheet, examples, KATA...

Get it

MapDB binaries are hosted in Maven Central repository. Here is dependency fragment for MapDB.

<dependency>
    <groupId>org.mapdb</groupId>
    <artifactId>mapdb</artifactId>
    <version>VERSION</version>
</dependency>

VERSION is the last version number from Maven Central. You can also find the current version on this image:

Daily builds are in snapshot repository. The version number for the latest snapshot is here.

<repositories>
    <repository>
        <id>sonatype-snapshots</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>

<dependencies>
    <dependency>
        <groupId>org.mapdb</groupId>
        <artifactId>mapdb</artifactId>
        <version>VERSION</version>
    </dependency>
</dependencies>

You can also download MapDB jar files directly from Maven Central. In that case keep on mind that MapDB depends on Eclipse Collections, Guava, Kotlin library and some other libraries. Here is full list of dependencies for each version.

Hello World

Hereafter is a simple example. It opens in-memory HashMap, it uses off-heap store and it is not limited by Garbage Collection:

//import org.mapdb.*
DB db = DBMaker.memoryDB().make();
ConcurrentMap map = db.hashMap("map").createOrOpen();
map.put("something", "here");

HashMap (and other collections) can be also stored in file. In this case the content can be preserved between JVM restarts. It is necessary to call DB.close() to protect file from data corruption. Other option is to enable transactions with write ahead log.

DB db = DBMaker.fileDB("file.db").make();
ConcurrentMap map = db.hashMap("map").createOrOpen();
map.put("something", "here");
db.close();

TODO Hello World examples do not cover the commits.

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

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();

Example projects

TODO example projects

Quick Tips

  • Memory mapped files are much faster and should be enabled on 64bit systems for better performance.
  • MapDB has Pump for fast bulk import of collections. It is much faster than to Map.put()
  • Transactions have a performance overhead, but without them the store gets corrupted if not closed properly.
  • Data stored in MapDB (keys and values) should be immutable. MapDB serializes objects on background.
  • MapDB needs compaction sometimes. Run DB.compact() or see background compaction options.

results matching ""

    No results matching ""