Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package examples;
 
 import org.mapdb.DB;
 
 import java.util.Map;

Demonstrates Atomic.Long usage as unique-key generator. In SQL terms it demonstrates unique IDs using AUTO_INCREMENT. Variable is atomically incremented and persisted after JVM shutdown.
    public static void main(String[] args) {
        DB db = DBMaker.newTempFileDB().make();
        //open or create new map
        Map<LongStringmap = db.getTreeMap("map");
        // open existing or create new Atomic record with given name
        // if no record with given name exist, new recid is created with value `0`
        Atomic.Long keyinc = db.getAtomicLong("map_keyinc");
        // Allocate new unique key to use in map
        // Atomic.Long will use `compare-and-swap` operation to atomically store incremented value
        // Key values can be used only for single insert
        // key == 1
        Long key = keyinc.incrementAndGet();
        map.put(key"some string");
        // insert second entry,
        // key==2
        map.put(keyinc.incrementAndGet(), "some other string");
        ..println(map);
    }
New to GrepCode? Check out our FAQ X